I've been trying to figure out this for a while but a lot of answers I can find now are out-dated (posts from > 6 years ago), or less related.
The question is really how to deal with database sessions properly in celery. My current setting is: I have a global DbEngine object that contains dsn, engine, and Session. so every time if I want to use a session, I just call sess = db.Session() and start to use them inside my Flask app. It looks like the following:
#db.py
class DbEngine:
def __init__(self, path, ...):
self.dsn = self.create_dsn_from_file(path)
self.engine = create_engine(self.dsn)
self.Session = scoped_session(sessionmaker(bind=self.engine))
Until I introduced celery to my app, often I got various errors (Protocol error, sslSocket errors, etc). And I could not reproduce them locally, and it often gets fixed if I simply add retries to those tasks (it usually goes through with 3 retries). So I suspect that this might be caused by the Shared Sessions.
Then I decided to change my session for the celery app: every time if I need a session, I will actually create a new engine, create a new Session, and return the newly created Session(). However, the similar but not exactly the same problem happened again (various protocol errors with different codes).
I saw that celery has its own SessionManager, but I couldn't find sample code of usage. I was thinking about the following structure:
# celery_app.py
celery = ...
session_manager = SessionManager()
engine, Session = session_manager.create_session(dsn)
and in the tasks I do:
# task_1.py
from celery_app import celery, Session
@celery.task
def tsk():
sess = Session()
sess.query(...)
...
sess.close()
But really not sure if it's the intended approach, since I am only calling the .create_session() once, and I don't know when and how I should call the other functions in the SessionManager.
For the background, I am using DB2.
If anyone knows how to properly use SessionManager or has experience in using session with SQLAlchemy that'd be much appreciated if you can provide any insights. Thank you in advance!