Skip to content

Commit ef3e42b

Browse files
aeroskumaraditya303
authored andcommitted
Add thread timeout constant
1 parent 49cceeb commit ef3e42b

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,27 @@ Running and stopping the loop
178178

179179
.. versionadded:: 3.6
180180

181-
.. coroutinemethod:: loop.shutdown_default_executor()
181+
.. coroutinemethod:: loop.shutdown_default_executor(timeout=None)
182182

183183
Schedule the closure of the default executor and wait for it to join all of
184184
the threads in the :class:`ThreadPoolExecutor`. After calling this method, a
185185
:exc:`RuntimeError` will be raised if :meth:`loop.run_in_executor` is called
186186
while using the default executor.
187187

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+
188195
Note that there is no need to call this function when
189196
:func:`asyncio.run` is used.
190197

191198
.. versionadded:: 3.9
192199

200+
.. versionchanged:: 3.12
201+
Added the *timeout* parameter.
193202

194203
Scheduling callbacks
195204
^^^^^^^^^^^^^^^^^^^^

Lib/asyncio/base_events.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,13 @@ async def shutdown_asyncgens(self):
561561
'asyncgen': agen
562562
})
563563

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+
"""
566571
self._executor_shutdown_called = True
567572
if self._default_executor is None:
568573
return
@@ -572,7 +577,13 @@ async def shutdown_default_executor(self):
572577
try:
573578
await future
574579
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)
576587

577588
def _do_shutdown(self, future):
578589
try:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add *timeout* parameter to :meth:`asyncio.loop.shutdown_default_executor`.

0 commit comments

Comments
 (0)