Celery beat在每个间隔向工人发送两次相同的任务



我在example_app->中有以下计划任务;tasks.py:

@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(
crontab(minute='*/1'),
test.s(),
)
@app.task
def test():
print('test')

然而,此计划任务每分钟执行两次:

celery_1       | [2022-02-08 16:53:00,044: INFO/MainProcess] Task example_app.tasks.test[a608d307-0ef8-4230-9586-830d0d900e67] received
celery_1       | [2022-02-08 16:53:00,046: INFO/MainProcess] Task example_app.tasks.test[5d5141cc-dcb5-4608-b115-295293c619a9] received
celery_1       | [2022-02-08 16:53:00,046: WARNING/ForkPoolWorker-6] test
celery_1       | [2022-02-08 16:53:00,047: WARNING/ForkPoolWorker-7] test
celery_1       | [2022-02-08 16:53:00,048: INFO/ForkPoolWorker-6] Task example_app.tasks.test[a608d307-0ef8-4230-9586-830d0d900e67] succeeded in 0.0014668999938294291s: None
celery_1       | [2022-02-08 16:53:00,048: INFO/ForkPoolWorker-7] Task example_app.tasks.test[5d5141cc-dcb5-4608-b115-295293c619a9] succeeded in 0.001373599996441044s: None

我读到,当你将django时区从UTC更改时,可能会导致这种情况,我在这个项目中已经这样做了。我从另一个问题尝试了这个解决方案,但它并没有阻止复制:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.base')

class MyCeleryApp(Celery):
def now(self):
"""Return the current time and date as a datetime."""
from datetime import datetime
return datetime.now(self.timezone)

app = MyCeleryApp('tasks')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

设置:

CELERY_BROKER_URL = "redis://redis:6379/0"
CELERY_RESULT_BACKEND = "redis://redis:6379/0"

在docker中运行:


celery:
restart: always
build:
context: .
command: celery --app=config.celery worker -l info
depends_on:
- db
- redis
- django
celery-beat:
restart: always
build:
context: .
command: celery --app=config.celery beat -l info
depends_on:
- db
- redis
- django

也尝试过这样的docker:

celery:
restart: always
build:
context: .
command: celery --app=config.celery worker -B -l info
depends_on:
- db
- redis
- django

结果相同。

不确定是什么原因造成的。

如果它能帮助其他人,我通过从tasks.py中删除定期任务并在设置中定义它来解决问题。因此,文件如下所示:tasks.py

@app.task
def test():
print('test')

设置.py

CELERY_BEAT_SCHEDULE = {
"test-task": {
"task": "example_app.tasks.test",
"args": (),
"schedule": crontab(),
}
}

不确定我以前的设置有什么问题,因为我认为它与文档一致。

相关内容

  • 没有找到相关文章

最新更新