Skip to content

Should triopg support manual resource management? #15

Open
@shamrin

Description

@shamrin

Today I was trying to do something that the documentation says is not supported:

triopg does not support manual resource management

And then I've realized I was the one who wrote the above 🤣 I think I was wrong. We should provide some form of manual resource management. Here is a simple web app using quart-trio. It uses the same pattern that Flask recommends for database connections:

from quart_trio import QuartTrio
from quart import current_app, g

app = QuartTrio(__name__)

async def get_db():
    if 'db' not in g:
        g.db = await current_app.config.db_pool.acquire()
    return g.db

@app.teardown_appcontext
async def teardown_db(exception):
    db = g.pop('db', None)
    if db is not None:
        await db.release()

Handle can then use get_db(). Each handler / task gets a separate database connection:

@app.route('/', methods=['POST'])
async def hello():
    db = await get_db()
    # ...

But, as I said, await pool.acquire() is not supported in triopg 🤷‍♂️:

    g.db = await current_app.config.db_pool.acquire()
TypeError: object TrioPoolAcquireContextProxy can't be used in 'await' expression

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions