8000 bpo-34037, asyncio: add BaseEventLoop.wait_executor_on_close (GH-13786) · python/cpython@0f0a30f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f0a30f

Browse files
authored
bpo-34037, asyncio: add BaseEventLoop.wait_executor_on_close (GH-13786)
Add BaseEventLoop.wait_executor_on_close attribute: true by default. loop.close() now waits for the default executor to finish by default. Set loop.wait_executor_on_close attribute to False to not wait for the executor.
1 parent 78c7d52 commit 0f0a30f

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,18 @@ Running and stopping the loop
140140
The loop must not be running when this function is called.
141141
Any pending callbacks will be discarded.
142142

143-
This method clears all queues and shuts down the executor, but does
144-
not wait for the executor to finish.
143+
This method clears all queues and shuts down the default executor. By
144+
default, it waits for the default executor to finish. Set
145+
*loop.wait_executor_on_close* to ``False`` to not wait for the executor.
145146

146147
This method is idempotent and irreversible. No other methods
147148
should be called after the event loop is closed.
148149

150+
.. versionchanged:: 3.8
151+
The method now waits for the default executor to finish by default.
152+
Added *loop.wait_executor_on_close* attribute.
153+
154+
149155
.. coroutinemethod:: loop.shutdown_asyncgens()
150156

151157
Schedule all currently open :term:`asynchronous generator` objects to

Lib/asyncio/base_events.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ async def wait_closed(self):
380380
class BaseEventLoop(events.AbstractEventLoop):
381381

382382
def __init__(self):
383+
# If true, close() waits for the default executor to finish
384+
self.wait_executor_on_close = True
383385
self._timer_cancelled_count = 0
384386
self._closed = False
385387
self._stopping = False
@@ -635,7 +637,7 @@ def close(self):
635637
executor = self._default_executor
636638
if executor is not None:
637639
self._default_executor = None
638-
executor.shutdown(wait=False)
640+
executor.shutdown(wait=self.wait_executor_on_close)
639641

640642
def is_closed(self):
641643
"""Returns True if the event loop was closed."""
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`asyncio`: ``loop.close()`` now waits for the default executor to
2+
finish by default. Set ``loop.wait_executor_on_close`` attribute to
3+
``False`` to opt-in for Python 3.7 behavior (not wait for the executor to
4+
finish).

0 commit comments

Comments
 (0)
0