芹菜工人围

2024-04-19 03:50:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Celery框架编写一个应用程序。我的一些任务相当重,可以执行很长时间。在

我注意到,当我运行5-6个工人,然后放置10-20个任务时,他们可能会随机分配给工人,有时如果一个人没有任务,就不会开始剩下的任务,只有当他们完成任务(可能在几个小时内)时,其他人才会处理这些任务。如果我现在再运行一个worker-它什么也不做,但是可以接受新任务。在

这是一个bug还是一个特性?我如何解决我的需求?当我们有空闲的工人而没有开始任务时,等待几个小时是没有意义的。在


Tags: 框架应用程序特性bug空闲celery意义worker
1条回答
网友
1楼 · 发布于 2024-04-19 03:50:21

它不是一个bug或一个特性(更有可能是一个特性),它只是配置错误。

正如documentation所说,工人可以为自己保留一些任务,以加快处理消息的速度。但这只对小而快速的任务有意义-它不向代理请求新消息,而是立即启动保留消息。

但对于长期任务来说,这可能会导致你的问题中所描述的情况。

If you have many tasks with a long duration you want the multiplier value to be 1, which means it will only reserve one task per worker process at a time.

If you have a combination of long- and short-running tasks, the best option is to use two worker nodes that are configured separately, and route the tasks according to the run-time.

因此,您需要在celery的设置中设置CELERYD_PREFETCH_MULTIPLIER = 1

但是

When using early acknowledgement (default), a prefetch multiplier of 1 means the worker will reserve at most one extra task for every active worker process.

When users ask if it’s possible to disable “prefetching of tasks”, often what they really want is to have a worker only reserve as many tasks as there are child processes.

我还建议将CELERY_ACKS_LATE = True设置为仅在任务完成后发送ACK命令。这样,工作线程就不会保留任何额外的任务,但当前正在执行的任务将被标记为仅保留。

尽管这有一个副作用-如果工作线程在执行任务的过程中崩溃/终止,任务将再次标记为未启动,任何其他工作线程都可以从头开始重新启动它。所以确保你有idempotent任务。请再看一次docs

相关问题 更多 >