Skip to content

Commit

Permalink
69th PR Nice - Fix Python 3.10 asyncio.gather exception (#69)
Browse files Browse the repository at this point in the history
* Fix Python 3.10 asyncio.gather exception

In Py 3.10 `asyncio.gather` wont' take the loop in as a kwarg, but rather use `_events._get_event_loop()` which, since there's no loop set, will raise an exception.
But this only happens if there are no tasks to be cancelled. If there are everything is going to be fine.
For simplicity I put the whole thing under the `if` statement, because running `asyncio.gather` with no tasks would have no effect on older versions either.

I suggest adding a regression test for this, at some point.
Just please merge it ASAP, because I don't like having modified vendor code :D

Also, thanks for the awesome lib, I'm thankful I got rid of the boilerplate.

* Update aiorun.py

Co-authored-by: Caleb Hattingh <caleb.hattingh@gmail.com>
  • Loading branch information
Pirulax and cjrh authored Apr 18, 2022
1 parent a07b5dc commit 70ce24b
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions aiorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,22 +318,15 @@ def sep():
return tasks, do_not_cancel

tasks, do_not_cancel = sep()
# Here's a protip: if you group a bunch of tasks, and some of them
# get cancelled, and they DON'T HANDLE THE CANCELLATION, then the
# raised CancelledError will bubble up to, and stop the
# loop.run_until_complete() line: meaning, not all the tasks in
# the gathered group will actually be complete. You need to
# enable this with the ``return_exceptions`` flag.
kwargs = dict(
return_exceptions=True
)
if sys.version_info < (3, 10):
kwargs['loop'] = loop
group = gather(*tasks, *do_not_cancel, **kwargs)
logger.info("Running pending tasks till complete")
# TODO: obtain all the results, and log any results that are exceptions
# other than CancelledError. Will be useful for troubleshooting.
loop.run_until_complete(group)

async def wait_for_cancelled_tasks():
return await gather(*tasks, *do_not_cancel, return_exceptions=True)

if tasks or do_not_cancel:
logger.info("Running pending tasks till complete")
# TODO: obtain all the results, and log any results that are exceptions
# other than CancelledError. Will be useful for troubleshooting.
loop.run_until_complete(wait_for_cancelled_tasks())

logger.info("Waiting for executor shutdown.")
executor.shutdown(wait=True)
Expand Down

0 comments on commit 70ce24b

Please sign in to comment.