-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crashes while performing asyncio.gather tasks #634
Comments
Check this issue #572 |
Yes, that's a good reference, thank you @Pentusha . To add more information to that, 2 facts to know first:
That means in your code, a lazy connection is stored in the context right before entering the By the way, Starlette (and some other frameworks) handles different requests in different tasks. And it is fine to run concurrent database queries in these tasks because they don't share the same context, thus they run queries in different database connections. To solve your issue, you could turn off DATABASE_CONFIG = {
....
'use_connection_for_request': False,
}
@app.get("/")
async def root():
async with db.acquire():
tasks = [db.scalar('SELECT now()') for i in range(5)]
result = await asyncio.gather(*tasks) Another more reliable solution is to enforce a new context for each subtask as @Pentusha Alternatively, you could explicitly acquire new connections in the subtasks like this: async def now():
async with db.acquire():
return await db.scalar("SELECT now()")
@app.get("/")
async def root():
async with db.acquire():
tasks = [now() for i in range(5)]
result = await asyncio.gather(*tasks) |
Thanks a lot, this solution works for me! |
Description
Here is a minimal FastApi+gino service:
What I Did
I try to run several queries concurrently, but get this error:
Are there any tricks to perform such operations in a proper way?
The text was updated successfully, but these errors were encountered: