Skip to content
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

Reusing connections and asyncio tasks #572

Closed
briskweb opened this issue Oct 16, 2019 · 2 comments
Closed

Reusing connections and asyncio tasks #572

briskweb opened this issue Oct 16, 2019 · 2 comments
Labels
question A community question, closed when inactive.

Comments

@briskweb
Copy link

  • GINO version: 0.8.2
  • Python version: 3.7.3
  • asyncpg version: 0.18.3
  • aiocontextvars version: 0.2.2

Description

When I have a reusing connection and create new tasks, the tasks inherit the same reusing connection, which may result in a conflict.

I would expect that you can only reuse connections from the same task and new tasks have no reusing connection on their stacks in the beginning. Is it possible to have this behavior?

What I Did

Here is a small example showing the problem. It throws
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress.

from gino import Gino
import asyncio

db = Gino()


async def main():
    await db.set_bind('postgresql+asyncpg://...')
    async with db.acquire(reuse=True) as con:
        t1 = asyncio.create_task(test())
        t2 = asyncio.create_task(test2())
        await asyncio.gather(t1, t2)


async def test():
    print(await db.scalar("SELECT pg_sleep(10)"))


async def test2():
    print(await db.scalar("SELECT pg_backend_pid()"))

asyncio.run(main())
@fantix
Copy link
Member

fantix commented Oct 16, 2019

This would work:

from aiocontextvars import Context

...

    async with db.acquire(reuse=True) as con:
        t1 = Context().run(asyncio.create_task, test())
        t2 = Context().run(asyncio.create_task, test2())

@fantix fantix added the question A community question, closed when inactive. label Oct 17, 2019
@wwwjfy
Copy link
Member

wwwjfy commented Nov 9, 2019

Closing due to inactivity. Feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A community question, closed when inactive.
Projects
None yet
Development

No branches or pull requests

3 participants