芹菜:未注册的任务

时间:2018-03-21 13:58:21

标签: python asynchronous celery

我试图关注Celery tutorial。我使用的是芹菜版4.1.0

|-module
  |-tasks
    |-__init__.py
    |-tasks.py

__init__.py的内容为空。我在tasks目录中创建了一个名为tasks.py的文件。

from celery import Celery

app = Celery("tasks", broker="redis://localhost:6379/0", backend="redis://localhost:6379/0")


@app.task
def add(x, y):
    return x + y

我启动了Redis服务器,在另一个终端窗口中,我已经启动了芹菜工作服务器:

celery worker -A module.tasks.tasks -l info -P eventlet

运行任务:

$ python
>>> from tasks import add
>>> add.delay(1, 1)

在服务器工作者终端窗口中,我收到以下错误:

[2018-03-21 13:46:48,859: ERROR/MainProcess] Received unregistered task of type 'module.tasks.tasks.add'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
b'[[1, 2], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (81b)
Traceback (most recent call last):
  File "C:\Users\Callu\Anaconda3\envs\project\lib\site-packages\celery\worker\consumer\consumer.py", line 561, in on_task_received
    strategy = strategies[type_]
KeyError: 'module.tasks.tasks.add'

更新 如果更改了tasks目录并尝试调用add函数,则可以正常工作。但是如果我尝试从另一个python文件调用add函数,它会输出与以前相同的错误消息。

2 个答案:

答案 0 :(得分:0)

首先要运行芹菜,确保你在void ButtonCapturePhoto_Clicked(object sender, EventArgs e) { Camera.InvokeOnData(your data); } 目录中,然后通过以下方式运行芹菜:

module/tasks/

答案 1 :(得分:0)

我这样开始吃芹菜:

python -m celery -A yourpackage #add your specific options here

以上意味着您的venv必须安装一个名为yourpackage的软件包。如果您不熟悉将代码打包为谷歌,请使用how to create setup.py或类似的短语。准备好包装后,可以像这样安装:

source /path/to/your/venv
cd /path/to/your/yourpackage
pip install -e .

在此阶段,芹菜希望您的设置文件位于celery.py文件中。我的celery.py看起来像这样:

from celery import Celery

app = Celery(
    'yourpackagename',
    include=[
        'tasks.tasks.add',
    ]
)
app.config_from_object('yourpackagename.celeryconfig')

现在你需要创建你的celeryconfig.py文件(它不必以这种方式调用,事实上你可能甚至不必这样做);我使用rabbitmq,所以你的值看起来会不同

# Setup backend
BACKEND = 'amqp'
BROKER_URL = 'amqp://myuser:mypassword@localhost:5672/myvhost'

此时您的文件夹结构如下所示:

|-yourpackagename
  |-setup.py
  |-yourpackagename
    |-celery.py
    |-celeryconfig.py
    |-tasks
      |-__init__.py
      |-tasks.py