Reputation: 2861
How can I use sqlalchemy models in celery delayed tasks ?
It looks like celery has this option https://github.com/celery/celery/blob/master/celery/backends/database/session.py , but I can't find an example how to use it.
@celery.task
def mytask(data):
# how to I get session here?
This thread didn't helped me.
Upvotes: 1
Views: 3846
Reputation: 10397
Any reason to not just create the session as a global variable and use it in the celery tasks?
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# an Engine, which the Session will use for connection
# resources
some_engine = create_engine('postgresql://scott:tiger@localhost/')
# create a configured "Session" class
Session = sessionmaker(bind=some_engine)
# create a Session
session = Session()
@celery.task
def mytask(data):
session.commit(data)
Upvotes: 2