New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asyncio: will shutdown_default_executor work in single step (stop, run_forever) mode? #84537
Comments
Is the new asyncio.loop.shutdown_default_executor() suitable for event loops that are run in single-step mode? event_loop.create_task(event_loop.shutdown_default_executor())
event_loop.stop()
event_loop.run_forever() I don't see how it will work since shutdown_default_executor() may not be finished during a single 'stopped' run_forever() call. Also, what happens to pending executor futures? Previously reported bug bpo-28464. Here is my currently working code for shutting down the event loop. # give event loop one chance to finish up
event_loop.stop()
event_loop.run_forever()
# wait for everything to finish, including tasks running in executors
# this assumes that all outstanding tasks finish in a reasonable time (i.e. no infinite loops).
all_tasks_fn = getattr(asyncio, "all_tasks", None)
if not all_tasks_fn:
all_tasks_fn = asyncio.Task.all_tasks
tasks = all_tasks_fn(loop=event_loop)
if tasks:
gather_future = asyncio.gather(*tasks, return_exceptions=True)
else:
# work around fact that gather always uses global event loop in Python 3.8
gather_future = event_loop.create_future()
gather_future.set_result([])
event_loop.run_until_complete(gather_future)
# due to a seeming bug in Python libraries, the default executor needs to be shutdown explicitly before the event loop
# see http://bugs.python.org/issue28464 .
_default_executor = getattr(event_loop, "_default_executor", None)
if _default_executor:
_default_executor.shutdown()
event_loop.close() |
I honestly can't say for certain; the primary intended use case for shutdown_default_executor() was to provide a means of properly finalizing the resources associated with the default executor without blocking the event loop, notably the threads in the ThreadPoolExecutor (which were intermittently left dangling). So, when working on the implementation w/ Yury and Andrew, I did not strongly consider single-step event loops. I was more concerned with how it fit in with asyncio.run() and safe finalization of executor resources. See https://bugs.python.org/issue34037 for context. If you have a recommendation for a change to the current version shutdown_default_executor() that would help provide compatibility with single-step event loops without hindering the primary goals, I'm sure it would be considered.
When using From a glance at the example code posted above, it seems like it would be incompatible with asyncio.run(), which is a requirement for shutdown_default_executor(). See cpython/Lib/asyncio/runners.py Line 8 in b9c46a2
|
This isn't a supported use-case. You are supposed to |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: