我尝试使用Celery(和rabbitmq)与Flask邮件异步发送电子邮件。最初,我遇到了烧瓶破坏芹菜render_template的问题 - 烧瓶邮件破坏芹菜(芹菜任务仍然可以成功执行,但没有发送电子邮件)。当我试图解决这个问题(仍然没有解决!)时 - 我偶然发现了另一个问题。这种酸洗错误是由于螺纹锁紧造成的。我注意到当我改变调用芹菜任务的方式(从延迟到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
tasks.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)
仅供参考:如果我不使用芹菜(即同步),API 工作得很好。提前感谢!
当您调用send_auth_email.delay(test_app, nick, email)
时,所有函数参数都将发送到任务队列。为此,芹菜腌制它们。
简答test_app
,在烧瓶中施用,使用一些魔法,不能腌制。有关哪些可以腌制,哪些不能腌制的详细信息,请参阅文档。
一种解决方案是传递所有必要的参数(在您的情况下,这似乎只是名称)以重新实例化test_app
send_auth_email
.