Skip to content

Right way to connect to mysql use connection pool in async falcon app #1981

Answered by vytas7
icebearrrr asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @icebearrrr!
I've never used aiomysql, so take this with a grain of salt, as I don't know what the "right" way to work with it is.

With the above disclaimer in mind, in ASGI, you can make use of startup and shutdown events, that are easy to implement with Falcon middleware. You can put async initialization code into a process_startup(...) method, as also showcased in our ASGI tutorial.

So we could create an aiomysql pool, and then either use process_request and process_response to automatically draw connections from pool and store them in req.context, or use the pool directly in a responder. The following works for me with both approaches:

import logging

import aiomysql
import falcon.a…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
6 replies
@CaselIT
Comment options

@vytas7
Comment options

@CaselIT
Comment options

@vytas7
Comment options

@CaselIT
Comment options

Answer selected by CaselIT
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants