我可以在没有django的情况下使用芹菜

Kar*_*arl 6 python django celery

我有API代码,它将任务添加到队列中,然后芹菜工作者消耗这些任务.

目前我的代码库都相同.但是我希望芹菜工人只是拥有简单的普通Python任务而没有django代码,因为工人只会处理任务而且不需要django.可能吗.

为了启动芹菜工人我需要使用这条线

celery -A django_project worker --queue high
Run Code Online (Sandbox Code Playgroud)

我该怎么写而不是django_project呢

Tht*_*htu 8

是的你可以.Celery是一个通用的异步任务队列.代替"django_project",你会指向你的模块.有关示例,请参见http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#application.

以下是使用celery的示例项目布局:

project-dir/
    mymodule/
         __init__.py 
         celery.py
         tasks.py
    tests/
    setup.py
    etc, etc (e.g. tox.ini, requirements.txt, project management files)
Run Code Online (Sandbox Code Playgroud)

在mymodule/celery.py中:

# -*- coding : utf-8 -*-
from __future__ import absolute_import

from celery import Celery

app = Celery('mymodule',
             broker='amqp://',
             backend='amqp://',
             include=['mymodule.tasks'])

if __name__ == '__main__':
    app.start()
Run Code Online (Sandbox Code Playgroud)

在mymodule/tasks.py中:

from __future__ import absolute_import

from mymodule.celery import app

@app.task
def add(x, y):
    return x + y
Run Code Online (Sandbox Code Playgroud)