You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Integrate the async transport with the rest of the SDK. Provide a new
experimental option `transport_async` that enables the async transport
if an event loop is running. Otherwise, fall back to the sync transport.
Furthermore, adapt the client to work with the async transport. To this
end, flush and close were changed to be non blocking and awaitable in an
async context to avoid deadlocks, however close enforces a completed
flush before shutdown. As there are to my knowledge no background
threads running flush/close, these methods are currently not
thread-safe/loop-aware for async, which can be changed if necessary.
Atexit issue: The atexit integration used by the SDK runs after the
event loop has already closed if asyncio.run() is used. This makes it
impossible for the async flush to happen, as atexit calls
client.close(), but a loop is no longer present. I attempted to apply
[this fix](https://discuss.python.org/t/atexit-for-asyncio/13911) by
patching the loop close in the asyncio integration, but I am unsure if I
did it correctly/put it in the correct spot, or if this is a good idea.
From my SDK test however, it seems to fix the flush issue. Note also
that this will apparently be patched in Python 3.14, as per the
discussion in the linked thread.
As a final note, I added event loop checking. Whenever the event loop is
used, the transport/client catch RuntimeErrors, which would be thrown in
case the event loop was already shut down. I am not sure if this is a
case we need to consider, but I added it for now because I did not want
the transport to potentially throw RuntimeError if the event loop is
shutdown during a program. If this should be left out currently for
simplicity, I can remove it again.
I added the [httpcore[asyncio] ](https://www.encode.io/httpcore/async/)
dependency to requirements-testing, as it is needed for the async
httpcore functionality.
GH-4601
---------
Co-authored-by: Neel Shah <[email protected]>
0 commit comments