【问题标题】:Can i use celery without django我可以在没有 django 的情况下使用芹菜吗
【发布时间】:2016-08-27 21:00:35
【问题描述】:

我有 API 代码可以将任务添加到队列中,然后 celery 工作人员会使用这些任务。

目前我有两个相同的代码库。但我希望 celery 工作人员只需执行简单的 Python 任务并且没有 django 代码,因为工作人员只会处理任务并且不需要 django。有没有可能。

为了启动 celery worker 我需要使用这条线

celery -A django_project worker --queue high

我应该写什么而不是 django_project 那里

【问题讨论】:

    标签: python django celery


    【解决方案1】:

    是的,你可以。 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)
    

    在 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()
    

    在 mymodule/tasks.py 中:

    from __future__ import absolute_import
    
    from mymodule.celery import app
    
    @app.task
    def add(x, y):
        return x + y
    

    【讨论】:

    • celery.py 的内容应该是什么
    猜你喜欢
    • 2017-12-31
    • 2019-01-31
    • 2016-08-11
    • 2021-08-10
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多