Will calling a sessionmaker or closing a session block?

45 views
Skip to first unread message

Chris Withers

unread,
Jun 2, 2019, 4:05:01 AM6/2/19
to sqlal...@googlegroups.com
Hi All,

Given this async function (asgi middleware, as it happens):

@app.middleware('http')
async def make_db_session(request: Request, call_next):
    request.state.db = Session()
    response = await call_next(request)
    request.state.db.close()
    return response

Would either the call to Session, which is a sessionmaker, or
db.close(), be expected to block?
I can't remember whether instantiating a session will open a network
connection, or whether close will similarly do network io, so thought
I'd ask...

Chris

Mike Bayer

unread,
Jun 2, 2019, 9:19:04 AM6/2/19
to sqlal...@googlegroups.com
So that's an empty Session, which won't do anything immediately, however if anything is done on it that uses a database, then that all blocks, and if so, db.close() can also block because it has to cleanup the database connection which is definitely blocking IO stuff.   If you didn't do anything with engines or connections, then db.close() won't block.

If you aren't doing anything with the database then I can't imagine why you'd want to use a Session inside of an async though.  
-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper


To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages