芹菜任务未经芹菜加工

时间:2017-12-20 14:17:07

标签: django python-3.x celery

总芹菜和django noob在这里,很抱歉,如果问题是微不足道的。基本上问题是@ app.task定义的任何函数都没有被芹菜处理,它只是正常运行,好像芹菜不在那里。

我的celery_app.py文件是 -

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

app = Celery(broker=settings.CELERY_BROKER_URL)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

if __name__ == '__main__':
    app.start()

虽然我的tasks.py文件是 -

from project.celery_app import app

@app.task
def mytask():
    ...

我在终端中运行芹菜时得到以下输出 -

 -------------- celery@LAPTOP v4.1.0 (latentcall)
---- **** -----
--- * ***  * -- Windows-10-10.0.16299-SP0 2017-12-20 19:27:24
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         __main__:0x229ce2884e0
- ** ---------- .> transport:   amqp://user:**@localhost:5672/myvhost
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 8 (solo)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this         worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . account.tasks.mytask

[2017-12-20 19:27:24,085: INFO/MainProcess] Connected to     amqp://user:**@127.0.0.1:5672/myvhost
[2017-12-20 19:27:24,101: INFO/MainProcess] mingle: searching for neighbors
[2017-12-20 19:27:25,126: INFO/MainProcess] mingle: all alone
[2017-12-20 19:27:25,141: WARNING/MainProcess]     c:\programdata\anaconda2\envs\myenv\lib\site-    packages\celery\fixups\django.py:202: UserWarning: Using settings.DEBUG leads to     a memory leak, never use this setting in production environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2017-12-20 19:27:25,141: INFO/MainProcess] celery@LAPTOP- ready.

所以芹菜知道我的任务,但它没有做任何事情。该任务在点击按钮上运行,并且使用-loglevel = debug可以看出芹菜不受其影响。我使用RabbitMQ作为经纪人,芹菜4.1.0,python3和django-1.10.5。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

正如我所想,一个简单的错误。只需将mytask()更改为mytask.delay(),芹菜就开始接收它了。

答案 1 :(得分:1)

.delay()实际上是一种shorcut方法。如果想要提供其他选项,则必须使用.apply_async()

官方文档可以在这里找到: http://docs.celeryproject.org/en/latest/userguide/calling.html