MCPcopy
hub / github.com/python-trio/trio / wait_all_threads_completed

Function wait_all_threads_completed

src/trio/_threads.py:87–112  ·  view source on GitHub ↗

Wait until no threads are still running tasks. This is intended to be used when testing code with trio.to_thread to make sure no tasks are still making progress in a thread. See the following code for a usage example:: async def wait_all_settled(): while True:

()

Source from the content-addressed store, hash-verified

85
86
87async 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
115def active_thread_count() -> int:

Calls 3

checkpointFunction · 0.85
getMethod · 0.45
waitMethod · 0.45

Tested by 2

wait_no_threads_leftFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…