我正在为 Python 寻找类似 cron 的分布式框架,并找到了 Celery。然而,文档说“你必须确保一次只有一个调度程序在为一个计划运行,否则你最终会得到重复的任务”,Celery 使用 celery.beat.PersistentScheduler 将计划存储到本地文件。
所以,我的问题是,除了默认设置之外,还有其他实现可以将调度“放入集群”并协调任务执行,以便每个任务只运行一次吗? 我的目标是能够在集群中的所有主机上以相同的时间表运行 celerybeat。
谢谢
最佳答案
tl;dr:没有 Celerybeat 不适合您的用例。您只需运行一个 celerybeat
进程,否则您的任务将重复。
我知道这是一个非常古老的问题。我会尝试做一个小总结,因为我有同样的问题/问题(在 2018 年)。
一些背景:我们在 Kubernetes 集群中运行 Django 应用程序(使用 Celery)。集群(EC2 实例)和 Pod(~容器)是自动缩放的:简单地说,我不知道应用程序的运行时间和数量。
您有责任只运行 celerybeat
的一个进程,否则,您的任务将被重复。 [1] Celery 存储库中有此功能请求:[2]
Requiring the user to ensure that only one instance of celerybeat exists across their cluster creates a substantial implementation burden (either creating a single point-of-failure or encouraging users to roll their own distributed mutex).
celerybeat should either provide a mechanism to prevent inadvertent concurrency, or the documentation should suggest a best-practice approach.
一段时间后,这个功能请求被 Celery 的作者以资源不足为由拒绝了。 [3] 我强烈建议阅读 Github 上的整个帖子。那里的人推荐这些项目/解决方案:
- https://github.com/ybrs/single-beat
- https://github.com/sibson/redbeat
- 使用锁定机制(http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html#ensuring-a-task-is-only-executed-one-at-a-time)
我没有尝试上面的任何操作(我不希望我的应用程序中有其他依赖项,我不喜欢锁定任务/你需要处理故障转移等/)。
我最终在 Kubernetes 中使用了 CronJob (https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/)。
[1] celerybeat - multiple instances & monitoring
[2] https://github.com/celery/celery/issues/251
[3] https://github.com/celery/celery/issues/251#issuecomment-228214951
关于python - 分布式 celery 调度器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011950/