Django Celery 邮件,Celery 无法工作

0 投票
1 回答
813 浏览
提问于 2025-04-20 21:31

我正在使用Django的Celery来发送注册邮件,但Celery运行得不太对劲。

我在Celery中遇到了错误。

[2014-09-10 19:11:44,349: WARNING/MainProcess] celery@Jeet-PC ready.
[2014-09-10 19:13:38,586: INFO/MainProcess] Received task:       apps.kashmiri.tasks.SignUpTask[17ca2ae1-8c72-426c-babd-470a55ac19
5]
[2014-09-10 19:13:38,936: ERROR/Worker-1] Pool process <Worker(Worker-1, started)> error:     RuntimeError(RuntimeError("App reg
stry isn't ready yet.",), <function model_unpickle at 0x034E4B30>, (('auth', 'User'), [],     <function simple_class_factory at
x034E4AF0>))
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\billiard\pool.py", line 289, in run
sys.exit(self.workloop(pid=pid))
File "C:\Python27\lib\site-packages\billiard\pool.py", line 350, in workloop
req = wait_for_job()
File "C:\Python27\lib\site-packages\billiard\pool.py", line 441, in receive
ready, req = _receive(1.0)
File "C:\Python27\lib\site-packages\billiard\pool.py", line 413, in _recv
return True, loads(get_payload())
File "C:\Python27\lib\site-packages\billiard\common.py", line 77, in pickle_loads
return load(BytesIO(s))
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 1450, in model_unpickle
model = apps.get_model(*model_id)
File "C:\Python27\lib\site-packages\django\apps\registry.py", line 187, in get_model
self.check_ready()
File "C:\Python27\lib\site-packages\django\apps\registry.py", line 119, in check_ready
raise RuntimeError("App registry isn't ready yet.")
RuntimeError: (RuntimeError("App registry isn't ready yet.",), <function model_unpickle at    0x034E4B30>, (('auth', 'User'), [
, <function simple_class_factory at 0x034E4AF0>))
[2014-09-10 19:13:39,233: ERROR/MainProcess] Process 'Worker-3' pid:7100 exited with 'signal -1'

forms.py

from apps.kashmiri.tasks import SignUpTask
SignUpTask.delay(user=user)

celery.py

from __future__ import absolute_import

import os

from celery import Celery
from django.conf import settings


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'kb.settings')

app=Celery('kb')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind= True)
def debug_task(self):
print('Request:{0!r}'.format(self.request))

tasks.py

from __future__ import absolute_import
from celery.registry import tasks
from celery.task import Task
from celery.app import shared_task
from django.utils.html import strip_tags
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string

class SignUpTask(Task):
def run(self, user):
    subject, from_email, to ='Welcome to Kashmiri Bhatta', 'info@seekersguru.com', user.email
    html_content = render_to_string('registration.html',{'user':user.email})
    text_content= strip_tags(html_content)
    msg= EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()

tasks.register(SignUpTask)

当我使用 >> celery -A proj worker -l info 这个命令时,

出现了错误 RuntimeError: (RuntimeError("应用程序注册还没准备好。",) 请帮我解决这个问题。

1 个回答

1

这个问题只出现在 Django 1.7 版本上。有人在github上报告了这个问题,最近已经被修复了,具体可以查看 这里。建议你升级到最新版本的Celery,这样应该就能解决这个问题。

pip install celery --upgrade 

撰写回答