【问题标题】:Where does CELERYBEAT_SCHEDULE go in your project?CELERYBEAT_SCHEDULE 在你的项目中去哪里了?
【发布时间】:2015-03-03 14:37:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: python celery celerybeat


    【解决方案1】:

    the docs 使它看起来像您的 CELERYBEAT_SCHEDULE 是一个单独的文件有点令人困惑。实际上,它是您的应用程序(又名:Celery() 实例)配置中的一个条目,您可以see it listed here。因此,无论您如何将配置添加到您的应用程序中,它都是如此。

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

    1. 把你的配置放到一个模块中,比如celeryconfig.py
    2. 导入定义或使用您的应用程序的模块。 import 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 attribute 'celery'

    我创建了this project 来演示 Celerybeat 的实际操作,如文档中所述。我相信它比单独阅读文档更好地展示了它是如何工作的。我还创建了this Docker image 以使其尽可能简单地启动和运行。

    【讨论】:

      猜你喜欢
      • 2014-02-02
      • 1970-01-01
      • 2015-11-17
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      相关资源
      最近更新 更多