我尝试使用 Celery(和 rabbitmq)与 Flask 邮件异步发送电子邮件。最初,我遇到了来自烧瓶破坏芹菜的渲染模板问题 - Flask-Mail 破坏了芹菜(芹菜任务仍将成功执行,但没有发送电子邮件)。当我试图解决这个问题时(仍然没有解决!) - 我偶然发现了另一个问题。这个酸洗错误是由于线程锁造成的。我注意到当我更改调用 celery 任务的方式(从延迟到 apply_async)时,问题就开始了。从那时起,我尝试恢复我的更改,但我仍然无法摆脱错误。任何有关任何问题的帮助将不胜感激。
追溯:
File "/Users/.../python2.7/site-packages/celery/app/amqp.py", line 250, in publish_task
**kwargs
File "/Users/.../lib/python2.7/site-packages/kombu/messaging.py", line 157, in publish
compression, headers)
File "/Users/.../lib/python2.7/site-packages/kombu/messaging.py", line 233, in _prepare
body) = encode(body, serializer=serializer)
File "/Users/.../lib/python2.7/site-packages/kombu/serialization.py", line 170, in encode
payload = encoder(data)
File "/Users/.../lib/python2.7/site-packages/kombu/serialization.py", line 356, in dumps
return dumper(obj, protocol=pickle_protocol)
PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed
任务.py
from __future__ import absolute_import
from flask import render_template
from flask.ext.mail import Message
from celery import Celery
celery = Celery('tasks',
broker = 'amqp://tester:testing@localhost:5672/test_host')
@celery.task(name = "send_async_email")
def send_auth_email(app, nickname, email):
with app.test_request_context("/"):
recipients = []
recipients.append(email)
subject = render_template("subject.txt")
msg = Message(subject, recipients = recipients)
msg.html = render_template("test.html", name = nickname)
app.mail.send(msg)
在测试用例中,我只是调用:
send_auth_email.delay(test_app, nick, email)
仅供参考:如果我不使用 celery(即同步),API 工作得非常好。提前致谢!