如何在芹菜延迟任务中使用sqlalchemy模型?
看起来芹菜有这个选项https://github.com/celery/celery/blob/master/celery/backends/database/session.py,但我找不到如何使用它的例子。
@celery.task
def mytask(data):
# how to I get session here?
This线程没有帮助我。
答案 0 :(得分:2)
有什么理由不将会话创建为全局变量并在芹菜任务中使用它?
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)