gpt4 book ai didi

python - CELERYBEAT_SCHEDULE 在你的项目中放在哪里?

转载 作者:太空狗 更新时间:2023-10-29 21:39:41 25 4
gpt4 key购买 nike

我多次阅读了几页 Google 搜索结果,我对如何布局我的项目感到非常困惑。我已经设法让 Celerybeat 使用 periodic_task 装饰器工作,但那是 depreciated and is being removed .据我了解,the docs建议 CELERYBEAT_SCHEDULE 是替代品。我已经完全按照所示创建了一个文件,但还没有弄明白:

  • 如何命名?
  • 放在哪里?

我发现很难找到在非 Django 项目中使用 Celery 的正确方法。

最佳答案

the docs 有点令人困惑让它看起来像你的 CELERYBEAT_SCHEDULE 是一个单独的文件。实际上,它是您应用程序(又名:Celery() 实例)配置中的一个条目 see it listed here .因此,无论您如何将配置放入您的应用程序,这就是它的去向。

有很多方法可以将配置放入应用程序中。如果您相信“Explicit is better than implicit”,那么您可能想要:

  1. 将您的配置放入模块中,例如 celeryconfig.py
  2. 导入定义或使用您的应用程序的模块。 导入celeryconfig
  3. 将模块对象应用于您的应用程序。 app.config_from_object(celeryconfig)

示例 celeryconfig.py 文件

from datetime import timedelta

BROKER_URL = "redis://redis.local:6379/0"
BROKER_TRANSPORT_OPTIONS = {'fanout_prefix': True, 'fanout_patterns': True, 'visibility_timeout': 480}
CELERY_RESULT_BACKEND = BROKER_URL

CELERYBEAT_SCHEDULE = {
'addrandom-to-16K-every-2-seconds': {
'task': 'celery_test.tasks.addrandom', # notice that the complete name is needed
'schedule': timedelta(seconds=2),
'args': (16000, 42)
},
}

CELERY_TIMEZONE = 'UTC'

尝试将其放入类似 beatschedule.py 的文件中,然后运行 ​​celery -A beatschedule beat 将得到 AttributeError: 'module' object has no属性“ celery ”

我创建了 this project按照文档中的描述演示 Celerybeat 的运行情况。我相信它比单独阅读文档更好地展示了它是如何工作的。我还创建了 this Docker image使其尽可能简单地启动和运行。

关于python - CELERYBEAT_SCHEDULE 在你的项目中放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834506/

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