-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
Per our discussion on Slack, I think that the Mongo Async client should just always use the current asyncio loop, and not bother caching the loop at all.
Currently the event loop is cached here, and this breaks reusing the client across event loops as in PYTHON-4724.
The logic for caching the loop predates asyncio async/await, which are features that inherently depend on an event loop running. The typical way to enter this state is by using asyncio.run(). It is as far as I know otherwise impossible or very difficult to enter execution of an async function from synchronous code, so it seems likely to be safe to always fetch the event loop. Also, calling code that needs to use an event loop without having an active thread-local event loop set up is an error, and a more obvious and expected one for users.
If async code does need to be called from a synchronous function then I believe the correct thing to do is probably to use asyncio.run, which should otherwise look very much like a blocking synchronous function to an outside caller.
- is related to
-
PYTHON-4724 Document the behavior of using an async client across multiple event loops
- Backlog
- related to
-
MOTOR-1334 Running async client inside different async io loops breaks the client
- Backlog