Description
Hi All,
Feature or enhancement
Make an async.get_running_loop() that returns None instead of raising RuntimeError. Or make asyncio._get_running_loop() public. Also make a asyncio.current_task() that returns None.
Pitch
Some frameworks, like Django use asyncio.get_running_loop() to detect whether or not the application has a running event loop, and fall back to using threads if not in async mode. This happen a lot across the codebase. The problem is in sync mode asyncio.get_running_loop() always raises RuntimeError, which is very inefficient. It would be much more efficient to get a None value returned. It ends up raising a RuntimeError whenever thread/task-local storage is used in non-async mode.
Example: https://github.com/django/asgiref/blob/main/asgiref/sync.py#L501-L510
https://github.com/django/asgiref/blob/main/asgiref/local.py#L44-L57
asyncio.run uses asyncio._get_running_loop(). Can other programs use it too?
asyncio.run() uses asyncio._get_running_loop() under the hood. https://github.com/python/cpython/blob/main/Lib/asyncio/runners.py#L188
Can asyncio._get_running_loop() be made public so other software can also the more efficient version? Can we get a version of asyncio.current_task() that returns None instead of raising a RuntimeError?
Thanks,
Collin
Metadata
Metadata
Assignees
Projects
Status

