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

[BUG] Bare connections cannot be used as context managers #583

Open
sm-Fifteen opened this issue Jun 3, 2020 · 1 comment
Open

[BUG] Bare connections cannot be used as context managers #583

sm-Fifteen opened this issue Jun 3, 2020 · 1 comment

Comments

@sm-Fifteen
Copy link

  • asyncpg version: 0.20.1
  • PostgreSQL version: 10.3
  • Python version: 3.8.2
  • Platform: Windows
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes

While under most use cases, people will want to use connection pools to manage their connections, there are times where using bare connections turns out to be more practical. However, while connection pools can be opened and closed via context managers:

async with asyncpg.create_pool(user='postgres', command_timeout=60) as pool:
    async with pool.acquire() as con:
        await con.fetch('SELECT 1')

...it appears that bare connections created via asyncpg.connect do not support this:

async with asyncpg.connect(user='postgres') as con:
    await con.fetch('SELECT 1')
File "./test.py", line 120, in fetch_ip_list
    async with asyncpg.connect(user="postgres") as con:
AttributeError: __aexit__

I personally like using with blocks to handle the scope of resources like this, so this is a step down for me in terms of ergonomics, not to mention rather surprising given that connections coming from connection pools do work as context managers (even though those are merely returned to the pool instead of closed).

@jmehnle
Copy link

jmehnle commented Sep 16, 2024

What's the hold-up on this? Not having looked at the code, my intuition is that this should be exceedingly easy to implement. I might contribute a PR on this, but I'd rather not waste my time. @elprans, would you welcome this feature or are you opposed to it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants