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

Context manager for ConnectionPool#connection method #34

Closed
Veritaris opened this issue Apr 17, 2024 · 3 comments
Closed

Context manager for ConnectionPool#connection method #34

Veritaris opened this issue Apr 17, 2024 · 3 comments

Comments

@Veritaris
Copy link

ATM to use connection we need to write the following code:

connection_pool = psqlpy.ConnectionPool()
conn = await connection_pool.connection()
conn.execute("SELECT * from users")

But it would be more pythonic to use context manager here like:

connection_pool = psqlpy.ConnectionPool()
async with connection_pool.connection() as conn:
    conn.execute("SELECT * from users")
@chandr-andr
Copy link
Member

Hi!
Thank you for the issue.

This idea is great, but It has some cons.

We have an asynchronous connection() method because we need to make an asynchronous call to the database pool.
When we get a connection that can be used in a context manager, it must have some magical method and access to the ConnectionPool.
If we want to have 2 ways of connection declaration, we need to be available to start it explicitly, like Transaction has a begin method.
Example:

# First without context manager
...
conn = connection_pool.connection()
await conn.start()

# Second with a context manager
...
async with connection_pool.connection() as conn:

It seems a bit complicated to me with some start method for connection.
If you have another opinion please share it.

@chandr-andr
Copy link
Member

I've started implementing this, but I'm faced with performance issues.
#36

So, it will take time to understand how to do it in another way.

@chandr-andr
Copy link
Member

It's implemented in release https://github.com/qaspen-python/psqlpy/releases/tag/0.7.0 with the acquire method.

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