执行异步任务时芹菜正在引发异常
#case where error is thrown
send_registration_email.delay("test", "test@gmail.com", {})
当我通过省略celery
执行代码时,不会出现此错误#case where code is executed correctly
send_registration_email("test", "test@gmail.com", {})
如何用芹菜执行我的异步任务,所以我会摆脱这个错误?
[2015-10-12 14:50:57,176: ERROR/MainProcess] Task tasks.core.email.send_registration_email[5f96bee3-9df7-42ce-b726-c7086e82b954] raised unexpected: NameError("global name 'Mailer' is not defined",)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/srv/www/compare/htdocs/tasks/core/email.py", line 6, in send_registration_email
@shared_task
NameError: global name 'Mailer' is not defined
# email.py
from __future__ import absolute_import
from celery import shared_task
from utilities.helpers.mailer import Mailer
@shared_task
def send_registration_email(email_type="", recipient="", data={}):
Mailer.send_email(email_type, recipient, data)
# mailer.py
from __future__ import absolute_import
from django.core.mail.message import EmailMultiAlternatives
from django.template.loader import get_template
class Mailer():
@staticmethod
def send_email(email_type="", recipient="", data={}):
try:
email = Mailer.create_email(email_type, recipient, data)
email.send()
return True
except Exception as e:
return False
@classmethod
def create_email(self, email_type, recipient, data):
subject = ""
message = ""
sender_email = "mailgun@xxx.mailgun.org"
if email_type == "test":
subject = "Test subject"
content_html = "<html><body><h1>Test</h1></body></html>"
email = EmailMultiAlternatives(subject, None, sender_email, [recipient])
email.attach_alternative(content_html, "text/html")
return email
答案 0 :(得分:1)
您需要重新启动工作程序才能查看工作实例中反映的文件更改。 Celery不监视文件更改,因此如果在工作程序运行时更改任何文件,则需要重新启动工作程序。
出于开发目的,可以使用auto reloading功能来回避这一点:
启用自动重新加载后,工作人员会启动另一个线程来监视文件系统中的更改。导入新模块,并且每当检测到更改时都会重新加载已导入的模块,并且如果使用prefork池,则子进程将完成他们正在执行的工作并退出,以便可以使用新进程替换它们,从而有效地重新加载代码
这是一项实验性功能,只应在开发环境中使用
答案 1 :(得分:0)
正如@grrrrrr建议重新启动芹菜工作者修复了这个问题。在我的情况下,芹菜工人由主管经营,所以我不得不重新启动主管。