Message366945
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 #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() |
|
Date |
User |
Action |
Args |
2020-04-21 22:00:47 | cmeyer | set | recipients:
+ cmeyer, vstinner |
2020-04-21 22:00:47 | cmeyer | set | messageid: <1587506447.82.0.117857372941.issue40357@roundup.psfhosted.org> |
2020-04-21 22:00:47 | cmeyer | link | issue40357 messages |
2020-04-21 22:00:47 | cmeyer | create | |
|