Celery:无限重复超时卡住(等待UP消息超时)

5 投票
1 回答
3672 浏览
提问于 2025-04-18 09:39

我定义了一些任务,时间限制是1200:

@celery.task(time_limit=1200)
def create_ne_list(text):
    c = Client()
    return c.create_ne_list(text)

我还使用了worker_process_init信号来进行一些初始化,每次新进程启动时都会执行:

@worker_process_init.connect
def init(sender=None, conf=None, **kwargs):
    init_system(celery.conf)
    init_pdf(celery.conf)

这个初始化函数执行需要几秒钟。

除此之外,我还使用了以下配置:

CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json']
BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'amqp://'
CELERY_TIMEZONE = 'Europe/Berlin'
CELERY_ENABLE_UTC = True

然后我用以下命令启动我的工作进程:

celery -A isc worker -l info --concurrency=3

正如预期的那样,启动工作进程时,初始化函数被调用了三次。现在,我可以发送任务,它们被执行,一切看起来都很顺利。

但是:一旦有任务超出了时间限制,工作进程就会陷入一个无限循环,不停地被创建又被杀掉,因为超出了时间限制。

[2014-06-13 09:46:18,978: ERROR/MainProcess] Timed out waiting for UP message from <Worker(Worker-20381, started daemon)>
[2014-06-13 09:46:20,000: ERROR/MainProcess] Process 'Worker-20381' pid:18953 exited with 'signal 9 (SIGKILL)'
// new worker 20382 getting started, initialization getting triggerd and soon after that -->
[2014-06-13 09:46:18,978: ERROR/MainProcess] Timed out waiting for UP message from <Worker(Worker-20382, started daemon)>
[2014-06-13 09:46:20,000: ERROR/MainProcess] Process 'Worker-20382' pid:18954 exited with 'signal 9 (SIGKILL)'
// and so on....

有没有人知道为什么会发生这种情况?

1 个回答

5

这个回答的意思是,信号 worker_process_init 要求处理程序的执行时间不能超过4秒。如果超过这个时间,就会出现问题。

http://celery.readthedocs.org/en/latest/userguide/signals.html#worker-process-init

因为我的初始化函数执行得比较慢,所以工作进程会自动被终止。之后,它会自然地重新启动,然后再次触发初始化函数,这样就又会导致工作进程被终止,反复循环下去。

撰写回答