【问题标题】:Failed to starting the worker process Celery task with Django in debug = False (production)无法在调试 = False(生产)中使用 Django 启动工作进程 Celery 任务
【发布时间】:2019-04-29 09:45:47
【问题描述】:

这是一个用于发送电子邮件的 Celery 任务,有趣的是在DEBUG = True 模式下一切正常,但在生产DEBUG = False 下却无法正常工作。在发送操作期间运行 celery worker -A invoice2 --loglevel = debug 并在激活的调试模式下显示以下内容:

 ERROR/ForkPoolWorker-5] Task enviar_documentos[xxx] raised unexpected: TypeError("enviar_documentos_async() got an unexpected keyword argument 'cliente'",)

首先是延迟开始发送的文件:

from django.conf import settings
from notifications.tasks import send_async_documents

def send_documents (company, client, documents, type, template = "invoice", context = None):
    send_documents = send_documents_async.delay
    send_documents (type = type,
                    company = company.pk,
                    client = client.pk,
                    template = template,
                    context = context)

二、任务:

from celery_app import app
@app.task (name = "send_documents")

def enviar_documentos_async (company, client, documents, type = "FacturaVenta", template = "invoice", context = None):
     if not context:
       context = {}
       klass = KLASS.get (type)
       queryset = klass.objects.filter (pk__in = documents)
       em = Empresa.objects.get (pk = company)
       cl = Cliente.objects.get (pk = client)
       .
       .
       return send_email(
           mail_from = "\"{0.name}\"><{0.email}>".format(em),
           mail_to = cl.email,
           type = template,
           context = context,
           attachments = attachments)

三、邮件的发送:

from django.core.mail import EmailMessage
from django.template import Context, Template

def send_email(mail_from, mail_to, type, context = None, attachments = None):
   subject = Template(subject.type).render(mail_context) .replace ("\ n", "")
   message = Template(type.body).render(mail_context)
   ...
   msg = EmailMessage (
          subject,
          linebreaks (message),
          mail_from,
          mail_to)

通过 pip 在 vi​​rtualenv 中安装的应用版本:

   celery 4.3.0

   Django 2.1.1

   django-extensions 2.0.7

   django-filter 2.1.0

   django-oauth-toolkit 1.1.3

   django-redis 4.10.0

   django-redis-cache 2.0.0

   djangorestframework 3.8.2

   redis 3.2.1

   redis-cache 0.1.5

   redis-structures 0.1.7

   kombu 4.5.0

在服务器 Linux-4.9.0-3-amd64-x86_64-with-debian-9.0 + nginx/1.10.2 + uwsgi 2.0.17.1:

Redis server v = 5.0.4 sha = 00000000: 0 malloc = jemalloc-5.1.0 bits = 64
Celery 4.3.0 (rhubarb)

Settings.py 的一部分

DJANGO_ROOT = dirname (abspath (__ file__))
SITE_HTDOCS = normpath (join (DJANGO_ROOT, '../htdocs'))
STATIC_ROOT = normpath (join (SITE_HTDOCS, 'static'))
STATIC_URL = '/static/'
MEDIA_ROOT = normpath (join (SITE_HTDOCS, 'media'))
MEDIA_URL = '/media/'
...
# CELERY
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

【问题讨论】:

    标签: django redis celery


    【解决方案1】:

    你在哪里调用enviar_documentos_async函数? 可能是您提供 client 参数(客户端而不是客户端)的错字。

    【讨论】:

    • 代码完美运行,问题出在系统celery worker -A theapp --loglevel=debug需要同样的更多配置
    • 已解决。使用Django在生产中(debug = False),Celery不能直接运行:celery worker -A myapp --loglevel=info,还需要启动Celery脚本docs.celeryproject.org/en/latest/userguide/…
    【解决方案2】:

    在生产中使用 Django debug = False,Celery 不能直接通过运行:celery worker -A myapp --loglevel=info 工作,还需要启动 Celery 脚本 daemonization

    bin/celery worker --help
    

    嵌入式节拍选项: -B, --beat 还运行 celery beat 定期任务调度程序。 请注意,必须只有一个实例 这项服务。 .. 注意:: -B 用于 发展目的。对于生产环境,你 需要单独启动celery beat。

    重要参数:-B celery worker -A myapp --loglevel=info -B

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 2017-05-06
      • 2011-06-19
      • 1970-01-01
      • 2016-01-14
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      相关资源
      最近更新 更多