AsyncClient() blocked event loop #3707
Replies: 1 comment
|
Here is an exact profiling breakdown of what is happening under the hood, why it blocks for 100–600ms on initial instantiation, and the recommended patterns to resolve it. Profiling: Where the 0.6s goesRunning Python's The blocking time comes almost entirely from two synchronous OS operations:
Why
|
Uh oh!
There was an error while loading. Please reload this page.
AsyncClient()itself is a synchronous function. Placing it inside anasync defcoroutine will block the main thread's event loop — you can see a warning by enabling asyncio's debug mode.Currently, this problem is solved in two ways: either using
asyncio.to_threadto avoid blocking during lazy initialization, or sharing the object after it has been asynchronously initialized.Should there be an official, native solution to non-blockingly initialize it within an
async defcoroutine using the syntaxasync with AsyncClient() as client:?All reactions