Celery beat 定时发送任务,但celery 只是在生产中时不时地处理它们
Celery beat sends task regularly, but celery only processes them from time to time in production
我有一个使用 redis 集成 celery 的 django 项目。
我的 celery worker 在本地开发中工作得很好,现在我正在生产中部署。
在守护进程之前,我想看看 celery 在服务器中的行为。问题是,celery beat 每分钟都会正确发送任务(按照我的计划),但工作人员似乎并不是每次都收到它。有时需要 4/5 分钟才能收到并处理任务。这怎么可能?我试过调试,但是资料很少。
查看我的设置:
settings.py
CELERY_TIMEZONE = 'Europe/Warsaw'
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
# Other Celery settings
CELERY_BEAT_SCHEDULE = {
'task-number-one': {
'task': 'predict_assistance.alerts.tasks.check_measures',
'schedule': crontab(minute='*/1'),
},
}
tasks.py
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task()
def check_measures():
print('doing something')
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
app = Celery('predict_assistance')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
在此我的生产日志:
[2020-03-11 16:09:00,028: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:09:00,038: INFO/MainProcess] Received task: predict_assistance.alerts.tasks.check_measures[86f5c999-a53c-44dc-b568-00d924b5da9e]
[2020-03-11 16:09:00,046: WARNING/ForkPoolWorker-3] doing something
[2020-03-11 16:09:00,047: INFO/ForkPoolWorker-3] predict_assistance.alerts.tasks.check_measures[86f5c999-a53c-44dc-b568-00d924b5da9e]: doing something logger
[2020-03-11 16:09:00,204: INFO/ForkPoolWorker-3] Task predict_assistance.alerts.tasks.check_measures[86f5c999-a53c-44dc-b568-00d924b5da9e] succeeded in 0.16194193065166473s: None
[2020-03-11 16:10:00,049: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:10:00,062: INFO/MainProcess] Received task: predict_assistance.alerts.tasks.check_measures[c7786f38-793f-45e6-abb2-1c901e345e8f]
[2020-03-11 16:10:00,072: WARNING/ForkPoolWorker-3] doing something
[2020-03-11 16:10:00,073: INFO/ForkPoolWorker-3] predict_assistance.alerts.tasks.check_measures[c7786f38-793f-45e6-abb2-1c901e345e8f]: doing something logger
[2020-03-11 16:10:00,242: INFO/ForkPoolWorker-3] Task predict_assistance.alerts.tasks.check_measures[c7786f38-793f-45e6-abb2-1c901e345e8f] succeeded in 0.17491870187222958s: None
[2020-03-11 16:11:00,054: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:12:00,032: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:13:00,035: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:14:00,046: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:14:00,053: INFO/MainProcess] Received task: predict_assistance.alerts.tasks.check_measures[e0b3ef2b-ba15-421c-9a0f-0ef9f3ebb22a]
[2020-03-11 16:14:00,065: WARNING/ForkPoolWorker-3] doing something
[2020-03-11 16:14:00,066: INFO/ForkPoolWorker-3] predict_assistance.alerts.tasks.check_measures[e0b3ef2b-ba15-421c-9a0f-0ef9f3ebb22a]: doing something logger
[2020-03-11 16:14:00,247: INFO/ForkPoolWorker-3] Task predict_assistance.alerts.tasks.check_measures[e0b3ef2b-ba15-421c-9a0f-0ef9f3ebb22a] succeeded in 0.1897202990949154s: None
你知道为什么会这样吗?
提前致谢
正如评论所建议的那样,解决方案是从 redis-server
服务切换到 rabbitmq-server
服务
我有一个使用 redis 集成 celery 的 django 项目。
我的 celery worker 在本地开发中工作得很好,现在我正在生产中部署。 在守护进程之前,我想看看 celery 在服务器中的行为。问题是,celery beat 每分钟都会正确发送任务(按照我的计划),但工作人员似乎并不是每次都收到它。有时需要 4/5 分钟才能收到并处理任务。这怎么可能?我试过调试,但是资料很少。
查看我的设置: settings.py
CELERY_TIMEZONE = 'Europe/Warsaw'
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
# Other Celery settings
CELERY_BEAT_SCHEDULE = {
'task-number-one': {
'task': 'predict_assistance.alerts.tasks.check_measures',
'schedule': crontab(minute='*/1'),
},
}
tasks.py
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task()
def check_measures():
print('doing something')
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
app = Celery('predict_assistance')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
在此我的生产日志:
[2020-03-11 16:09:00,028: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:09:00,038: INFO/MainProcess] Received task: predict_assistance.alerts.tasks.check_measures[86f5c999-a53c-44dc-b568-00d924b5da9e]
[2020-03-11 16:09:00,046: WARNING/ForkPoolWorker-3] doing something
[2020-03-11 16:09:00,047: INFO/ForkPoolWorker-3] predict_assistance.alerts.tasks.check_measures[86f5c999-a53c-44dc-b568-00d924b5da9e]: doing something logger
[2020-03-11 16:09:00,204: INFO/ForkPoolWorker-3] Task predict_assistance.alerts.tasks.check_measures[86f5c999-a53c-44dc-b568-00d924b5da9e] succeeded in 0.16194193065166473s: None
[2020-03-11 16:10:00,049: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:10:00,062: INFO/MainProcess] Received task: predict_assistance.alerts.tasks.check_measures[c7786f38-793f-45e6-abb2-1c901e345e8f]
[2020-03-11 16:10:00,072: WARNING/ForkPoolWorker-3] doing something
[2020-03-11 16:10:00,073: INFO/ForkPoolWorker-3] predict_assistance.alerts.tasks.check_measures[c7786f38-793f-45e6-abb2-1c901e345e8f]: doing something logger
[2020-03-11 16:10:00,242: INFO/ForkPoolWorker-3] Task predict_assistance.alerts.tasks.check_measures[c7786f38-793f-45e6-abb2-1c901e345e8f] succeeded in 0.17491870187222958s: None
[2020-03-11 16:11:00,054: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:12:00,032: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:13:00,035: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:14:00,046: INFO/Beat] Scheduler: Sending due task task-number-one (predict_assistance.alerts.tasks.check_measures)
[2020-03-11 16:14:00,053: INFO/MainProcess] Received task: predict_assistance.alerts.tasks.check_measures[e0b3ef2b-ba15-421c-9a0f-0ef9f3ebb22a]
[2020-03-11 16:14:00,065: WARNING/ForkPoolWorker-3] doing something
[2020-03-11 16:14:00,066: INFO/ForkPoolWorker-3] predict_assistance.alerts.tasks.check_measures[e0b3ef2b-ba15-421c-9a0f-0ef9f3ebb22a]: doing something logger
[2020-03-11 16:14:00,247: INFO/ForkPoolWorker-3] Task predict_assistance.alerts.tasks.check_measures[e0b3ef2b-ba15-421c-9a0f-0ef9f3ebb22a] succeeded in 0.1897202990949154s: None
你知道为什么会这样吗?
提前致谢
正如评论所建议的那样,解决方案是从 redis-server
服务切换到 rabbitmq-server
服务