使用 Django+Celery 进行开发而不运行 `celeryd`?
在开发中,运行 celeryd 以及 Django 开发服务器有点麻烦。例如,是否可以要求 celery 在开发过程中同步运行任务?或者类似的东西?
In development, it's a bit of a hassle to run the celeryd
as well as the Django development server. Is it possible to, for example, ask celery
to run tasks synchronously during development? Or something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以通过在设置中设置
CELERY_TASK_ALWAYS_EAGER = True
来实现此目的。(常量以前称为
CELERY_ALWAYS_EAGER
)http://docs.celeryproject.org/en/最新/userguide/configuration.html#task-execution-settings
Yes you can do this by setting
CELERY_TASK_ALWAYS_EAGER = True
in your settings.(Constant was previously called
CELERY_ALWAYS_EAGER
)http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-execution-settings
django-celery 中还有一个自定义 Django 测试运行器,可以帮助处理 CELERY_ALWAYS_EAGER。
阅读有关在 Django 中使用 Celery 的更多信息 文档。
There's also a custom Django test runner in django-celery that helps with CELERY_ALWAYS_EAGER.
Read more about using Celery with Django on the docs.
在 Celery 的
4.0
版本中,CELERY_ALWAYS_EAGER
设置已替换为 Djangosettings.py
或task_always_eager< 中的
CELERY_TASK_ALWAYS_EAGER
/code> 原生位于celeryconf
中。由于 celery 配置从 3.x 版本到 4.x 版本的大量更改分布在许多行中,因此我建议使用内置的设置迁移工具。
celery升级设置--django
来源:
http:// docs.celeryproject.org/en/latest/whatsnew-4.0.html#step-4-upgrade-to-celery-4-0
In version
4.0
of CeleryCELERY_ALWAYS_EAGER
setting was replaced byCELERY_TASK_ALWAYS_EAGER
in Djangosettings.py
ortask_always_eager
natively incelery conf
.Since the numerous changes in the celery configuration from version 3.x to 4.x are spread over many lines, I suggest to use the built-in settings migration tool.
celery upgrade settings --django
source:
http://docs.celeryproject.org/en/latest/whatsnew-4.0.html#step-4-upgrade-to-celery-4-0
经过 5 年多的编写 Celery 任务后,我注意到我开发的一种模式可以帮助测试和简化开发 - 我意识到,如果我的 Celery 任务是我通常放入 < 中的常规 Python 函数的薄包装器,那就更好了代码>myproject.impl包。 Celery 任务可能包含一些严格与 Celery 相关的逻辑,例如使用分布式锁定、显式重试逻辑等。
After 5+ years of writing Celery tasks I have noticed a pattern I have developed that can help with testing and ease of development - I realised it is much better if my Celery tasks are thin wrappers around the regular Python functions that I typically put in
myproject.impl
package. Celery tasks may contain some strictly Celery related logic, like using distributed locking for an example, explicit retry logic, etc.