简体   繁体   English

Celery 的“autodiscover_tasks”函数的目的是什么?

[英]What is the purpose of Celery's "autodiscover_tasks" function?

I'm wondering what is the purpose of Celery's autodiscover_tasks function.我想知道 Celery 的autodiscover_tasks函数的目的是什么。 Im am using Celery 4.1.2 with Django 2.1.4.我正在使用 Celery 4.1.2 和 Django 2.1.4。

The Celery documentation refers to imports:芹菜文档指的是进口:

foo.tasks and bar.tasks being imported foo.tasksbar.tasks被导入

But I can't comprehend how this works.但我无法理解这是如何工作的。

All the examples I found on GitHub including this one from the Official Celery repo , rely on manually importing (ie from demoapp.tasks import add, mul, xsum ) the tasks even when calling the autodiscover_tasks function when booting the worker.我在 GitHub 上找到的所有示例,包括这个来自官方 Celery存储库的示例,都依赖于手动导入(即from demoapp.tasks import add, mul, xsum )任务,即使在启动 worker 时调用autodiscover_tasks函数也是如此。

I guess this is how Python work, you can't access to classes "globally", like in Ruby, for example.我想这就是 Python 的工作方式,您无法“全局”访问类,例如在 Ruby 中。

Then once again, what is this function for?那么再一次,这个功能是做什么用的? I'm no expert at Celery and maybe I am missing something.我不是 Celery 的专家,也许我遗漏了什么。 The only thing I see is the name of the discovered tasks when launching the Celery worker, is that all this function is supposed to do?我唯一看到的是启动 Celery worker 时发现的任务的名称,所有这些功能都是应该做的吗?

Thanks for your inputs,感谢您的投入,

When using celery with django, the autodiscover_tasks function registers all decorated tasks within the task module inside each INSTALLED_APPS entry.将 celery 与 django 一起使用时, autodiscover_tasks函数会在每个INSTALLED_APPS条目内的任务模块中注册所有装饰任务。 eg,例如,

if your INSTALLED_APPS settings included app1, app2, and app3, celery would automatically register any decorated tasks that could be found by looking at app1.tasks , app2.tasks , and app3.tasks .如果您的INSTALLED_APPS设置包括 app1、app2 和 app3,celery 会自动注册任何可以通过查看app1.tasksapp2.tasksapp3.tasks找到的修饰任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM