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
{{ message }}
Repository navigation
Call async request several times with the same AsyncClient with asyncio.run() is causing RuntimeError: Event loop is closed #2473
When AsyncClient is used several times with an async function that's sending an http request, the first time is successful, all other attempts are failing with RuntimeError exception and "Event loop is closed" error message.
To reproduce:
import httpx
if name == 'main':
client = httpx.AsyncClient()
async def home():
r = await client.get('https://www.example.com/')
return r
asyncio.run(home())
asyncio.run(home()) # => RuntimeError: Event loop is closed
asyncio.run(home()) # => RuntimeError: Event loop is closed
asyncio.run(home()) # => RuntimeError: Event loop is closed
changed the title [-]Call async request sevreal times with the same AsyncClient with asyncio.run() is causing RuntimeError: Event loop is closed[/-][+]Call async request several times with the same AsyncClient with asyncio.run() is causing RuntimeError: Event loop is closed[/+]on Dec 1, 2022
Don't try to share an AsyncClient across multiple event loops. That won't work, and I'd assume we probably wouldn't be able to make it work.
You either want...
asyncdefhome():
asyncwithhttpx.AsyncClient() asclient:
r=awaitclient.get('https://www.example.com/')
returnr# Not sure why you'd want to do this, but at least it's not broken now...asyncio.run(home())
asyncio.run(home())
asyncio.run(home())
asyncio.run(home())
Or more sensibly...
asyncdefhome(client):
r=awaitclient.get('https://www.example.com/')
returnrasyncdefmain():
# Structured correctly.# Single client shared across multiple requests.asyncwithhttpx.Client() asclient:
awaithome(client)
awaithome(client)
awaithome(client)
awaithome(client)
asyncio.run(main())
Activity
[-]Call async request sevreal times with the same AsyncClient with asyncio.run() is causing RuntimeError: Event loop is closed[/-][+]Call async request several times with the same AsyncClient with asyncio.run() is causing RuntimeError: Event loop is closed[/+]lovelydinosaur commentedon Dec 2, 2022
So, yeah...
Don't try to share an
AsyncClientacross multiple event loops. That won't work, and I'd assume we probably wouldn't be able to make it work.You either want...
Or more sensibly...