我正在尝试设置Celery任务。我们的主要应用程序是Pyramid with SQLAlchemy。
所以我的任务定义为:
from celery.contrib.methods import task
from apipython.celerytasks import celery
class Email():
def __init__(self, from_name, from_email, to_name, to_email, subject, html_body,
sendgrid_category=None):
self.from_name = from_name
self.from_email = from_email
self.to_name = to_name
self.to_email = to_email
self.subject = subject
self.body = None
self.html_body = html_body
self.sendgrid_category = sendgrid_category
class EmailService():
@task()
def task__send_smtp(self, email, from_user_id=None, to_user_id=None):
# send the email, not shown here
# EmailLog is a SQLAlchemy model
email_log = EmailLog(
email.subject,
email.html_body,
from_user_id=from_user_id,
to_user_id=to_user_id,
action_type=email.sendgrid_category)
DBSession.add(email_log)
transaction.commit()
和celerytasks.py我有:
from celery import Celery
celery = Celery('apipython.celery',
broker='sqla+mysql+mysqldb://root:notarealpassword@127.0.0.1/gs?charset=utf8',
backend=None,
include=['apipython.services.NotificationService'])
if __name__ == '__main__':
celery.start()
它有效 - 任务被序列化并被拾取。
但是当我尝试在任务中使用SQLAlchemy / DBSession时,我收到一个错误:
UnboundExecutionError: Could not locate a bind configured on mapper Mapper|EmailLog|emaillogs or this Session
我了解工作人员任务是在一个单独的进程上运行,需要设置其设置,会话,引擎等。所以我有这个:
@worker_init.connect
def bootstrap_pyramid(signal, sender):
import os
from pyramid.paster import bootstrap
sender.app.settings = bootstrap('development.ini')['registry'].settings
customize_settings(sender.app.settings)
engine = sqlalchemy.create_engine('mysql+mysqldb://root:notarealpassword@127.0.0.1/gs?charset=utf8')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
但是我仍然遇到同样的错误。
DBSession和Base在models.py中定义为
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
我想让模型绑定工作的步骤是什么?
第二个问题,这个用于创建会话/绑定的代码可以在celery的init和vs init中工作吗?
(顺便说一下,我确实尝试过pyramid_celery,但更喜欢做普通的芹菜)
谢谢,
答案 0 :(得分:1)
我的同事尝试了完全相同的代码并且有效。奇怪