File tree Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -178,18 +178,27 @@ Running and stopping the loop
178
178
179
179
.. versionadded :: 3.6
180
180
181
- .. coroutinemethod :: loop.shutdown_default_executor()
181
+ .. coroutinemethod :: loop.shutdown_default_executor(timeout=None )
182
182
183
183
Schedule the closure of the default executor and wait for it to join all of
184
184
the threads in the :class: `ThreadPoolExecutor `. After calling this method, a
185
185
:exc: `RuntimeError ` will be raised if :meth: `loop.run_in_executor ` is called
186
186
while using the default executor.
187
187
188
+ The *timeout * parameter specifies the amount of time the threadpool will
189
+ be given to finish joining. The default value is ``None ``, which means the
190
+ threadpool will be given an indefinite amount of time.
191
+
192
+ If the timeout duration is reached, a warning is emitted and threadpool is
193
+ terminated without waiting for its threads to finish joining.
194
+
188
195
Note that there is no need to call this function when
189
196
:func: `asyncio.run ` is used.
190
197
191
198
.. versionadded :: 3.9
192
199
200
+ .. versionchanged :: 3.12
201
+ Added the *timeout * parameter.
193
202
194
203
Scheduling callbacks
195
204
^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change @@ -561,8 +561,13 @@ async def shutdown_asyncgens(self):
561
561
'asyncgen' : agen
562
562
})
563
563
564
- async def shutdown_default_executor (self ):
565
- """Schedule the shutdown of the default executor."""
564
+ async def shutdown_default_executor (self , timeout = None ):
565
+ """Schedule the shutdown of the default executor.
566
+
567
+ The timeout parameter specifies the amount of time the threadpool will
568
+ be given to finish joining. The default value is None, which means
569
+ that the threadpool will be given an indefinite amount of time.
570
+ """
566
571
self ._executor_shutdown_called = True
567
572
if self ._default_executor is None :
568
573
return
@@ -572,7 +577,13 @@ async def shutdown_default_executor(self):
572
577
try :
573
578
await future
574
579
finally :
575
- thread .join ()
580
+ thread .join (timeout )
581
+
582
+ if thread .is_alive ():
583
+ warnings .warn ("The ThreadPoolExecutor did not finishing joining "
584
+ f"its threads within { timeout } seconds." ,
585
+ RuntimeWarning , stacklevel = 2 )
586
+ self ._default_executor .shutdown (wait = False )
576
587
577
588
def _do_shutdown (self , future ):
578
589
try :
Original file line number Diff line number Diff line change
1
+ Add *timeout * parameter to :meth: `asyncio.loop.shutdown_default_executor `.
You can’t perform that action at this time.
0 commit comments