python - 如何构建 celery 任务

标签 python celery

我有 2 种类型的任务:异步任务和计划任务。所以,这是我的目录结构:

proj
  |
  -- tasks
      |
      -- __init__.py
      |
      -- celeryapp.py     => celery instance defined in this file.
      |
      -- celeryconfig.py
      |
      -- async
      |    |
      |    -- __init__.py
      |    |
      |    -- task1.py    => from proj.tasks.celeryapp import celery
      |    |
      |    -- task2.py    => from proj.tasks.celeryapp import celery
      |
      -- schedule
           |
           -- __init__.py
           |
           -- task1.py    => from proj.tasks.celeryapp import celery
           |
           -- task2.py    => from proj.tasks.celeryapp import celery

但是当我像下面这样运行 celery worker 时,它不起作用。它不能接受来自 celery beat scheduler 的任务。

 $ celery worker --app=tasks -Q my_queue,default_queue

那么,有没有关于多任务文件组织的最佳实践?

最佳答案

基于 celery documentation您可以像这样导入 celery 任务的结构:

例如,如果您有一个(想象的)这样的目录树:

|
|-- foo
|    |-- __init__.py
|    |-- tasks.py
|
|-- bar
     |-- __init__.py
     |-- tasks.py

然后调用 app.autodiscover_tasks(['foo', bar']) 将导致模块 foo.tasks 和 bar.tasks 被导入。

关于python - 如何构建 celery 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17702578/

相关文章:

python - Pyramid 1.4 需要 WebOb 1.2b3+。 Google App Engine 提供 WebOb 1.1.1。难道这一切都要以泪水结束吗?

python - django celery 发送多封电子邮件

python - Celery为每个任务实例安排了带有到期时间的任务?

python - celery 花仪表板删除 worker

python - 用户消息的消息队列

python - Pandas ,删除重复的日期时间

python - 使用 zip 扩展迭代后检查可迭代?

python - 具有交叉熵损失的 Softmax 激活导致两个类别的输出分别准确地收敛于 0 和 1

python - 本地网络中 Python 中的 RabbitMQ、ZMQ 和 Celery 之间的最佳选择是什么?

python - 为多个 Django 项目使用单个 Celery 服务器