Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-4978

Async Mongo Client should always use the current thread-static event loop

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            john.daniels@mongodb.com John Daniels
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: