gpt4 book ai didi

python - 自动发现 celery 任务

转载 作者:行者123 更新时间:2023-12-02 06:52:54 24 4
gpt4 key购买 nike

我有以下项目树(用于测试目的),我试图了解 Celery 如何加载任务。

app
├── __init__.py
├── app.py
├── celery.py
└── my_tasks
├── __init__.py
└── tasks.py

celery.py 包含以下用于创建 Celery 实例的代码:

from celery import Celery

app = Celery("app", backend="rpc://", broker="redis://localhost:6379/0")
app.autodiscover_tasks()

tasks.py 创建一个任务:

from app.celery import app
from time import sleep


@app.task
def run_tests_for_hash():
# task code here

app.py 包含一个 FastAPI 应用程序,该应用程序具有一个端点来创建任务

from fastapi import FastAPI
from app.call_flow_tasks.tasks import run_tests_for_hash

app = FastAPI(title="Testing Studio runners")

@app.post("/create_task")
def create_task():

results = run_tests_for_hash.delay()

return {"task_id": results.id, "status": results.status}

但是尝试从命令行运行 Celery 工作程序时出现错误:

$celery -A app worker
Traceback (most recent call last):
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/bin/celery", line 8, in <module>
sys.exit(main())
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/__main__.py", line 16, in main
_main()
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/celery.py", line 322, in main
cmd.execute_from_commandline(argv)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/celery.py", line 495, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/base.py", line 305, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/celery.py", line 487, in handle_argv
return self.execute(command, argv)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/celery.py", line 419, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/worker.py", line 223, in run_from_argv
return self(*args, **options)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/base.py", line 253, in __call__
ret = self.run(*args, **kwargs)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/bin/worker.py", line 258, in run
**kwargs)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/worker/worker.py", line 96, in __init__
self.app.loader.init_worker()
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/loaders/base.py", line 114, in init_worker
self.import_default_modules()
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/loaders/base.py", line 108, in import_default_modules
raise response
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/utils/dispatch/signal.py", line 288, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/vine/promises.py", line 170, in __call__
return self.throw()
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/vine/promises.py", line 167, in __call__
retval = fun(*final_args, **final_kwargs)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/app/base.py", line 695, in _autodiscover_tasks
return self._autodiscover_tasks_from_fixups(related_name)
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/app/base.py", line 705, in _autodiscover_tasks_from_fixups
pkg for fixup in self._fixups
File "/Users/avlachopoulos/.venvs/test-runner-JK5CNUCV/lib/python3.7/site-packages/celery/app/base.py", line 706, in <listcomp>
for pkg in fixup.autodiscover_tasks()
AttributeError: 'NoneType' object has no attribute 'autodiscover_tasks'

命令在与 app python 包相同的级别上执行。如果我删除 autodiscover 它会正常工作并且工作人员会正常加载。关于 celery 如何自动发现任务以及如何从不同模块加载任务有任何帮助吗?

最佳答案

您可以组合两种类型的自动发现:

app.autodiscover_tasks()  # Find tasks using celery.fixups (i.e. Django apps via INSTALLED_APPS).
app.autodiscover_tasks( # Add other tasks not included in the apps.
[
'project.other.tasks',
'another_project.another.app',
]
)

Celery 只有一个 BUILTIN_FIXUPS = {'celery.fixups.django:fixup'},看它是 code to understand .

    def autodiscover_tasks(self):
from django.apps import apps
return [config.name for config in apps.get_app_configs()]

关于python - 自动发现 celery 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60503668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com