Debian Codegraph @

src/trio/_threads.py

raw
1 from __future__ import annotations
2
3 import contextlib
4 import contextvars
5 import inspect
6 import queue as stdlib_queue
7 import threading
8 from itertools import count
9 from typing import TYPE_CHECKING, Generic, TypeVar
10
11 import attrs
12 import outcome
13 from attrs import define
14 from sniffio import current_async_library_cvar
15
16 import trio
17
18 from ._core import (
19 RunVar,
20 TrioToken,
21 checkpoint,
22 disable_ki_protection,
23 enable_ki_protection,
24 start_thread_soon,
25 )
26 from ._sync import CapacityLimiter, Event
27 from ._util import coroutine_or_error
28
29 if TYPE_CHECKING:
30 from collections.abc import Awaitable, Callable, Generator
31
32 from typing_extensions import TypeVarTuple, Unpack
33
34 from trio._core._traps import RaiseCancelT
35
36 Ts = TypeVarTuple("Ts")
37
38 RetT = TypeVar("RetT")
39
40
41 class _ParentTaskData(threading.local):
42 """Global due to Threading API, thread local storage for data related to the
43 parent task of native Trio threads."""
44
45 token: TrioToken
46 abandon_on_cancel: bool
47 cancel_register: list[RaiseCancelT | None]
48 task_register: list[trio.lowlevel.Task | None]
49
50
51 PARENT_TASK_DATA = _ParentTaskData()
52
53 _limiter_local: RunVar[CapacityLimiter] = RunVar("limiter")
54 # I pulled this number out of the air; it isn't based on anything. Probably we
55 # should make some kind of measurements to pick a good value.
56 DEFAULT_LIMIT = 40
57 _thread_counter = count()
58
59
60 @define
61 class _ActiveThreadCount:
62 count: int
63 event: Event
64
65
66 _active_threads_local: RunVar[_ActiveThreadCount] = RunVar("active_threads")
67
68
69 @contextlib.contextmanager
70 def _track_active_thread() -> Generator[None, None, None]:
71 try:
72 active_threads_local = _active_threads_local.get()
73 except LookupError:
74 active_threads_local = _ActiveThreadCount(0, Event())
75 _active_threads_local.set(active_threads_local)
76
77 active_threads_local.count += 1
78 try:
79 yield
80 finally:
81 active_threads_local.count -= 1
82 if active_threads_local.count == 0:
83 active_threads_local.event.set()
84 active_threads_local.event = Event()
85
86
87 async def wait_all_threads_completed() -> None:
88 """Wait until no threads are still running tasks.
89
90 This is intended to be used when testing code with trio.to_thread to
91 make sure no tasks are still making progress in a thread. See the
92 following code for a usage example::
93
94 async def wait_all_settled():
95 while True:
96 await trio.testing.wait_all_threads_complete()
97 await trio.testing.wait_all_tasks_blocked()
98 if trio.testing.active_thread_count() == 0:
99 break
100 """
101
102 await checkpoint()
103
104 try:
105 active_threads_local = _active_threads_local.get()
106 except LookupError:
107 # If there would have been active threads, the
108 # _active_threads_local would have been set
109 return
110
111 while active_threads_local.count != 0:
112 await active_threads_local.event.wait()
113
114
115 def active_thread_count() -> int:
116 """Returns the number of threads that are currently running a task
117
118 See `trio.testing.wait_all_threads_completed`
119 """
120 try:
121 return _active_threads_local.get().count
122 except LookupError:
123 return 0
124
125
126 def current_default_thread_limiter() -> CapacityLimiter:
127 """Get the default `~trio.CapacityLimiter` used by
128 `trio.to_thread.run_sync`.
129
130 The most common reason to call this would be if you want to modify its
131 :attr:`~trio.CapacityLimiter.total_tokens` attribute.
132
133 """
134 try:
135 limiter = _limiter_local.get()
136 except LookupError:
137 limiter = CapacityLimiter(DEFAULT_LIMIT)
138 _limiter_local.set(limiter)
139 return limiter
140
141
142 # Eventually we might build this into a full-fledged deadlock-detection
143 # system; see https://github.com/python-trio/trio/issues/182
144 # But for now we just need an object to stand in for the thread, so we can
145 # keep track of who's holding the CapacityLimiter's token.
146 @attrs.frozen(eq=False, slots=False)
147 class ThreadPlaceholder:
148 name: str
149
150
151 # Types for the to_thread_run_sync message loop
152 @attrs.frozen(eq=False, slots=False)
153 class Run(Generic[RetT]): # type: ignore[explicit-any]
154 afn: Callable[..., Awaitable[RetT]] # type: ignore[explicit-any]
155 args: tuple[object, ...]
156 context: contextvars.Context = attrs.field(
157 init=False,
158 factory=contextvars.copy_context,
159 )
160 queue: stdlib_queue.SimpleQueue[outcome.Outcome[RetT]] = attrs.field(
161 init=False,
162 factory=stdlib_queue.SimpleQueue,
163 )
164
165 @disable_ki_protection
166 async def unprotected_afn(self) -> RetT:
167 coro = coroutine_or_error(self.afn, *self.args)
168 return await coro
169
170 async def run(self) -> None:
171 # we use extra checkpoints to pick up and reset any context changes
172 task = trio.lowlevel.current_task()
173 old_context = task.context
174 task.context = self.context.copy()
175 await trio.lowlevel.cancel_shielded_checkpoint()
176 result = await outcome.acapture(self.unprotected_afn)
177 task.context = old_context
178 await trio.lowlevel.cancel_shielded_checkpoint()
179 self.queue.put_nowait(result)
180
181 async def run_system(self) -> None:
182 result = await outcome.acapture(self.unprotected_afn)
183 self.queue.put_nowait(result)
184
185 def run_in_host_task(self, token: TrioToken) -> None:
186 task_register = PARENT_TASK_DATA.task_register
187
188 def in_trio_thread() -> None:
189 task = task_register[0]
190 assert task is not None, "guaranteed by abandon_on_cancel semantics"
191 trio.lowlevel.reschedule(task, outcome.Value(self))
192
193 token.run_sync_soon(in_trio_thread)
194
195 def run_in_system_nursery(self, token: TrioToken) -> None:
196 def in_trio_thread() -> None:
197 try:
198 trio.lowlevel.spawn_system_task(
199 self.run_system,
200 name=self.afn,
201 context=self.context,
202 )
203 except RuntimeError: # system nursery is closed
204 self.queue.put_nowait(
205 outcome.Error(trio.RunFinishedError("system nursery is closed")),
206 )
207
208 token.run_sync_soon(in_trio_thread)
209
210
211 @attrs.frozen(eq=False, slots=False)
212 class RunSync(Generic[RetT]): # type: ignore[explicit-any]
213 fn: Callable[..., RetT] # type: ignore[explicit-any]
214 args: tuple[object, ...]
215 context: contextvars.Context = attrs.field(
216 init=False,
217 factory=contextvars.copy_context,
218 )
219 queue: stdlib_queue.SimpleQueue[outcome.Outcome[RetT]] = attrs.field(
220 init=False,
221 factory=stdlib_queue.SimpleQueue,
222 )
223
224 @disable_ki_protection
225 def unprotected_fn(self) -> RetT:
226 ret = self.context.run(self.fn, *self.args)
227
228 if inspect.iscoroutine(ret):
229 # Manually close coroutine to avoid RuntimeWarnings
230 ret.close()
231 raise TypeError(
232 "Trio expected a synchronous function, but {!r} appears to be "
233 "asynchronous".format(getattr(self.fn, "__qualname__", self.fn)),
234 )
235
236 return ret
237
238 def run_sync(self) -> None:
239 result = outcome.capture(self.unprotected_fn)
240 self.queue.put_nowait(result)
241
242 def run_in_host_task(self, token: TrioToken) -> None:
243 task_register = PARENT_TASK_DATA.task_register
244
245 def in_trio_thread() -> None:
246 task = task_register[0]
247 assert task is not None, "guaranteed by abandon_on_cancel semantics"
248 trio.lowlevel.reschedule(task, outcome.Value(self))
249
250 token.run_sync_soon(in_trio_thread)
251
252 def run_in_system_nursery(self, token: TrioToken) -> None:
253 token.run_sync_soon(self.run_sync)
254
255
256 @enable_ki_protection
257 async def to_thread_run_sync(
258 sync_fn: Callable[[Unpack[Ts]], RetT],
259 *args: Unpack[Ts],
260 thread_name: str | None = None,
261 abandon_on_cancel: bool = False,
262 limiter: CapacityLimiter | None = None,
263 ) -> RetT:
264 """Convert a blocking operation into an async operation using a thread.
265
266 These two lines are equivalent::
267
268 sync_fn(*args)
269 await trio.to_thread.run_sync(sync_fn, *args)
270
271 except that if ``sync_fn`` takes a long time, then the first line will
272 block the Trio loop while it runs, while the second line allows other Trio
273 tasks to continue working while ``sync_fn`` runs. This is accomplished by
274 pushing the call to ``sync_fn(*args)`` off into a worker thread.
275
276 From inside the worker thread, you can get back into Trio using the
277 functions in `trio.from_thread`.
278
279 Args:
280 sync_fn: An arbitrary synchronous callable.
281 *args: Positional arguments to pass to sync_fn. If you need keyword
282 arguments, use :func:`functools.partial`.
283 abandon_on_cancel (bool): Whether to abandon this thread upon
284 cancellation of this operation. See discussion below.
285 thread_name (str): Optional string to set the name of the thread.
286 Will always set `threading.Thread.name`, but only set the os name
287 if pthread.h is available (i.e. most POSIX installations).
288 pthread names are limited to 15 characters, and can be read from
289 ``/proc/<PID>/task/<SPID>/comm`` or with ``ps -eT``, among others.
290 Defaults to ``{sync_fn.__name__|None} from {trio.lowlevel.current_task().name}``.
291 limiter (None, or CapacityLimiter-like object):
292 An object used to limit the number of simultaneous threads. Most
293 commonly this will be a `~trio.CapacityLimiter`, but it could be
294 anything providing compatible
295 :meth:`~trio.CapacityLimiter.acquire_on_behalf_of` and
296 :meth:`~trio.CapacityLimiter.release_on_behalf_of` methods. This
297 function will call ``acquire_on_behalf_of`` before starting the
298 thread, and ``release_on_behalf_of`` after the thread has finished.
299
300 If None (the default), uses the default `~trio.CapacityLimiter`, as
301 returned by :func:`current_default_thread_limiter`.
302
303 **Cancellation handling**: Cancellation is a tricky issue here, because
304 neither Python nor the operating systems it runs on provide any general
305 mechanism for cancelling an arbitrary synchronous function running in a
306 thread. This function will always check for cancellation on entry, before
307 starting the thread. But once the thread is running, there are two ways it
308 can handle being cancelled:
309
310 * If ``abandon_on_cancel=False``, the function ignores the cancellation and
311 keeps going, just like if we had called ``sync_fn`` synchronously. This
312 is the default behavior.
313
314 * If ``abandon_on_cancel=True``, then this function immediately raises
315 `~trio.Cancelled`. In this case **the thread keeps running in
316 background** – we just abandon it to do whatever it's going to do, and
317 silently discard any return value or errors that it raises. Only use
318 this if you know that the operation is safe and side-effect free. (For
319 example: :func:`trio.socket.getaddrinfo` uses a thread with
320 ``abandon_on_cancel=True``, because it doesn't really affect anything if a
321 stray hostname lookup keeps running in the background.)
322
323 The ``limiter`` is only released after the thread has *actually*
324 finished – which in the case of cancellation may be some time after this
325 function has returned. If :func:`trio.run` finishes before the thread
326 does, then the limiter release method will never be called at all.
327
328 .. warning::
329
330 You should not use this function to call long-running CPU-bound
331 functions! In addition to the usual GIL-related reasons why using
332 threads for CPU-bound work is not very effective in Python, there is an
333 additional problem: on CPython, `CPU-bound threads tend to "starve out"
334 IO-bound threads <https://bugs.python.org/issue7946>`__, so using
335 threads for CPU-bound work is likely to adversely affect the main
336 thread running Trio. If you need to do this, you're better off using a
337 worker process, or perhaps PyPy (which still has a GIL, but may do a
338 better job of fairly allocating CPU time between threads).
339
340 Returns:
341 Whatever ``sync_fn(*args)`` returns.
342
343 Raises:
344 Exception: Whatever ``sync_fn(*args)`` raises.
345
346 """
347 await trio.lowlevel.checkpoint_if_cancelled()
348 # raise early if abandon_on_cancel.__bool__ raises
349 # and give a new name to ensure mypy knows it's never None
350 abandon_bool = bool(abandon_on_cancel)
351 if limiter is None:
352 limiter = current_default_thread_limiter()
353
354 # Holds a reference to the task that's blocked in this function waiting
355 # for the result – or None if this function was cancelled and we should
356 # discard the result.
357 task_register: list[trio.lowlevel.Task | None] = [trio.lowlevel.current_task()]
358 # Holds a reference to the raise_cancel function provided if a cancellation
359 # is attempted against this task - or None if no such delivery has happened.
360 cancel_register: list[RaiseCancelT | None] = [None]
361 name = f"trio.to_thread.run_sync-{next(_thread_counter)}"
362 placeholder = ThreadPlaceholder(name)
363
364 # This function gets scheduled into the Trio run loop to deliver the
365 # thread's result.
366 def report_back_in_trio_thread_fn(result: outcome.Outcome[RetT]) -> None:
367 def do_release_then_return_result() -> RetT:
368 # release_on_behalf_of is an arbitrary user-defined method, so it
369 # might raise an error. If it does, we want that error to
370 # replace the regular return value, and if the regular return was
371 # already an exception then we want them to chain.
372 try:
373 return result.unwrap()
374 finally:
375 limiter.release_on_behalf_of(placeholder)
376
377 result = outcome.capture(do_release_then_return_result)
378 if task_register[0] is not None:
379 trio.lowlevel.reschedule(task_register[0], outcome.Value(result))
380
381 current_trio_token = trio.lowlevel.current_trio_token()
382
383 if thread_name is None:
384 thread_name = f"{getattr(sync_fn, '__name__', None)} from {trio.lowlevel.current_task().name}"
385
386 def worker_fn() -> RetT:
387 PARENT_TASK_DATA.token = current_trio_token
388 PARENT_TASK_DATA.abandon_on_cancel = abandon_bool
389 PARENT_TASK_DATA.cancel_register = cancel_register
390 PARENT_TASK_DATA.task_register = task_register
391 try:
392 ret = context.run(sync_fn, *args)
393
394 if inspect.iscoroutine(ret):
395 # Manually close coroutine to avoid RuntimeWarnings
396 ret.close()
397 raise TypeError(
398 "Trio expected a sync function, but {!r} appears to be "
399 "asynchronous".format(getattr(sync_fn, "__qualname__", sync_fn)),
400 )
401
402 return ret
403 finally:
404 del PARENT_TASK_DATA.token
405 del PARENT_TASK_DATA.abandon_on_cancel
406 del PARENT_TASK_DATA.cancel_register
407 del PARENT_TASK_DATA.task_register
408
409 context = contextvars.copy_context()
410 # Trio doesn't use current_async_library_cvar, but if someone
411 # else set it, it would now shine through since
412 # sniffio.thread_local isn't set in the new thread. Make sure
413 # the new thread sees that it's not running in async context.
414 context.run(current_async_library_cvar.set, None)
415
416 def deliver_worker_fn_result(result: outcome.Outcome[RetT]) -> None:
417 # If the entire run finished, the task we're trying to contact is
418 # certainly long gone -- it must have been cancelled and abandoned
419 # us. Just ignore the error in this case.
420 with contextlib.suppress(trio.RunFinishedError):
421 current_trio_token.run_sync_soon(report_back_in_trio_thread_fn, result)
422
423 await limiter.acquire_on_behalf_of(placeholder)
424 with _track_active_thread():
425 try:
426 start_thread_soon(worker_fn, deliver_worker_fn_result, thread_name)
427 except:
428 limiter.release_on_behalf_of(placeholder)
429 raise
430
431 def abort(raise_cancel: RaiseCancelT) -> trio.lowlevel.Abort:
432 # fill so from_thread_check_cancelled can raise
433 # 'raise_cancel' will immediately delete its reason object, so we make
434 # a copy in each thread
435 cancel_register[0] = raise_cancel
436 if abandon_bool:
437 # empty so report_back_in_trio_thread_fn cannot reschedule
438 task_register[0] = None
439 return trio.lowlevel.Abort.SUCCEEDED
440 else:
441 return trio.lowlevel.Abort.FAILED
442
443 while True:
444 # wait_task_rescheduled return value cannot be typed
445 msg_from_thread: outcome.Outcome[RetT] | Run[object] | RunSync[object] = (
446 await trio.lowlevel.wait_task_rescheduled(abort)
447 )
448 if isinstance(msg_from_thread, outcome.Outcome):
449 return msg_from_thread.unwrap()
450 elif isinstance(msg_from_thread, Run):
451 await msg_from_thread.run()
452 elif isinstance(msg_from_thread, RunSync):
453 msg_from_thread.run_sync()
454 else: # pragma: no cover, internal debugging guard TODO: use assert_never
455 raise TypeError(
456 f"trio.to_thread.run_sync received unrecognized thread message {msg_from_thread!r}.",
457 )
458 del msg_from_thread
459
460
461 def from_thread_check_cancelled() -> None:
462 """Raise `trio.Cancelled` if the associated Trio task entered a cancelled status.
463
464 Only applicable to threads spawned by `trio.to_thread.run_sync`. Poll to allow
465 ``abandon_on_cancel=False`` threads to raise :exc:`~trio.Cancelled` at a suitable
466 place, or to end abandoned ``abandon_on_cancel=True`` threads sooner than they may
467 otherwise.
468
469 Raises:
470 Cancelled: If the corresponding call to `trio.to_thread.run_sync` has had a
471 delivery of cancellation attempted against it, regardless of the value of
472 ``abandon_on_cancel`` supplied as an argument to it.
473 RuntimeError: If this thread is not spawned from `trio.to_thread.run_sync`.
474
475 .. note::
476
477 To be precise, :func:`~trio.from_thread.check_cancelled` checks whether the task
478 running :func:`trio.to_thread.run_sync` has ever been cancelled since the last
479 time it was running a :func:`trio.from_thread.run` or :func:`trio.from_thread.run_sync`
480 function. It may raise `trio.Cancelled` even if a cancellation occurred that was
481 later hidden by a modification to `trio.CancelScope.shield` between the cancelled
482 `~trio.CancelScope` and :func:`trio.to_thread.run_sync`. This differs from the
483 behavior of normal Trio checkpoints, which raise `~trio.Cancelled` only if the
484 cancellation is still active when the checkpoint executes. The distinction here is
485 *exceedingly* unlikely to be relevant to your application, but we mention it
486 for completeness.
487 """
488 try:
489 raise_cancel = PARENT_TASK_DATA.cancel_register[0]
490 except AttributeError:
491 raise RuntimeError(
492 "this thread wasn't created by Trio, can't check for cancellation",
493 ) from None
494 if raise_cancel is not None:
495 raise_cancel()
496
497
498 def _send_message_to_trio(
499 trio_token: TrioToken | None,
500 message_to_trio: Run[RetT] | RunSync[RetT],
501 ) -> RetT:
502 """Shared logic of from_thread functions"""
503 token_provided = trio_token is not None
504
505 if not token_provided:
506 try:
507 trio_token = PARENT_TASK_DATA.token
508 except AttributeError:
509 raise RuntimeError(
510 "this thread wasn't created by Trio, pass kwarg trio_token=...",
511 ) from None
512 elif not isinstance(trio_token, TrioToken):
513 raise RuntimeError("Passed kwarg trio_token is not of type TrioToken")
514
515 # Avoid deadlock by making sure we're not called from Trio thread
516 try:
517 trio.lowlevel.current_task()
518 except RuntimeError:
519 pass
520 else:
521 raise RuntimeError("this is a blocking function; call it from a thread")
522
523 if token_provided or PARENT_TASK_DATA.abandon_on_cancel:
524 message_to_trio.run_in_system_nursery(trio_token)
525 else:
526 message_to_trio.run_in_host_task(trio_token)
527
528 return message_to_trio.queue.get().unwrap()
529
530
531 def from_thread_run(
532 afn: Callable[[Unpack[Ts]], Awaitable[RetT]],
533 *args: Unpack[Ts],
534 trio_token: TrioToken | None = None,
535 ) -> RetT:
536 """Run the given async function in the parent Trio thread, blocking until it
537 is complete.
538
539 Returns:
540 Whatever ``afn(*args)`` returns.
541
542 Returns or raises whatever the given function returns or raises. It
543 can also raise exceptions of its own:
544
545 Raises:
546 RunFinishedError: if the corresponding call to :func:`trio.run` has
547 already completed, or if the run has started its final cleanup phase
548 and can no longer spawn new system tasks.
549 Cancelled: If the original call to :func:`trio.to_thread.run_sync` is cancelled
550 (if *trio_token* is None) or the call to :func:`trio.run` completes
551 (if *trio_token* is not None) while ``afn(*args)`` is running,
552 then *afn* is likely to raise :exc:`trio.Cancelled`.
553 RuntimeError: if you try calling this from inside the Trio thread,
554 which would otherwise cause a deadlock, or if no ``trio_token`` was
555 provided, and we can't infer one from context.
556 TypeError: if ``afn`` is not an asynchronous function.
557
558 **Locating a TrioToken**: There are two ways to specify which
559 `trio.run` loop to reenter:
560
561 - Spawn this thread from `trio.to_thread.run_sync`. Trio will
562 automatically capture the relevant Trio token and use it
563 to re-enter the same Trio task.
564 - Pass a keyword argument, ``trio_token`` specifying a specific
565 `trio.run` loop to re-enter. This is useful in case you have a
566 "foreign" thread, spawned using some other framework, and still want
567 to enter Trio, or if you want to use a new system task to call ``afn``,
568 maybe to avoid the cancellation context of a corresponding
569 `trio.to_thread.run_sync` task. You can get this token from
570 :func:`trio.lowlevel.current_trio_token`.
571 """
572 return _send_message_to_trio(trio_token, Run(afn, args))
573
574
575 def from_thread_run_sync(
576 fn: Callable[[Unpack[Ts]], RetT],
577 *args: Unpack[Ts],
578 trio_token: TrioToken | None = None,
579 ) -> RetT:
580 """Run the given sync function in the parent Trio thread, blocking until it
581 is complete.
582
583 Returns:
584 Whatever ``fn(*args)`` returns.
585
586 Returns or raises whatever the given function returns or raises. It
587 can also raise exceptions of its own:
588
589 Raises:
590 RunFinishedError: if the corresponding call to `trio.run` has
591 already completed.
592 RuntimeError: if you try calling this from inside the Trio thread,
593 which would otherwise cause a deadlock or if no ``trio_token`` was
594 provided, and we can't infer one from context.
595 TypeError: if ``fn`` is an async function.
596
597 **Locating a TrioToken**: There are two ways to specify which
598 `trio.run` loop to reenter:
599
600 - Spawn this thread from `trio.to_thread.run_sync`. Trio will
601 automatically capture the relevant Trio token and use it when you
602 want to re-enter Trio.
603 - Pass a keyword argument, ``trio_token`` specifying a specific
604 `trio.run` loop to re-enter. This is useful in case you have a
605 "foreign" thread, spawned using some other framework, and still want
606 to enter Trio, or if you want to use a new system task to call ``fn``,
607 maybe to avoid the cancellation context of a corresponding
608 `trio.to_thread.run_sync` task.
609 """
610 return _send_message_to_trio(trio_token, RunSync(fn, args))