简体   繁体   English

使用倒数计时,celery任务未出现在task.apply_async之后的队列中

[英]Celery task does not appear in queue after task.apply_async using countdown

I Hope this question qualifies for stack overflow. 我希望这个问题符合堆栈溢出的条件。

Working with celery 4.2.0 and Redis as broker and backend. 使用celery 4.2.0和Redis作为代理和后端。

Having a task 有任务

@shared_task()
def add(a, b):
    return a+b

And while a worker is active , running the fallowing command: 工作人员处于活动状态时 ,运行休闲命令:

add.apply_async(countdown=60)

Results in the task not being registered to the default celery queue, but still being executed after the period of time stated in countdown 结果任务未注册到默认芹菜队列,但在倒计时中指定的时间段后仍执行

Why is that, and how can I look for all pending tasks? 为什么会这样,我如何查找所有待处理的任务? Doing this would have worked if the task would be registered to the queue: 如果将任务注册到队列,则这样做会起作用:

    with celery_app.pool.acquire(block=True) as conn:
        tasks = conn.default_channel.client.lrange('celery', 0, -1)

If I terminate the worker while task havent been started I get the fallowing: 如果在任务尚未开始时终止工作器,则会得到休假:

[WARNING/MainProcess] Restoring 1 unacknowledged message(s)

This tells me the task is kept somewhere else other then the queue, but I cannot figure out where 这告诉我任务被保留在队列之外的其他位置,但是我无法弄清楚在哪里

Turns out this is actually the expected behavior as workers grab pending tasks even before execution to speed things up and thus remove them from queue. 事实证明,这实际上是预期的行为,因为工作人员甚至在执行之前就抓住未完成的任务以加快处理速度,从而将其从队列中删除。

To achieve what I wanted, which is disable workers from taking tasks from queues before actually starting to work on them, and losing track of that task, I had to use 2 celery settings together 为了实现我想要的目标,即禁止工作人员从队列中开始实际执行任务之前就开始执行任务,并且无法跟踪该任务,我不得不一起使用2个芹菜设置

task_acks_late = True
worker_prefetch_multiplier = 1

This way a task is removed from its original queue, but still exists in a queue called 'unacked', which allows me to monitor it. 这样,任务便从其原始队列中删除,但仍存在于称为“未确认”的队列中,这使我可以对其进行监视。 Optimizing celery 优化芹菜

Be advised that using the 'acks_late' acks late setting has some side affects you need to be aware of, such that if a worker unexpectedly terminated, the task will be restored and re run next time the worker is revived. 请注意,使用“ acks_late” acks Late设置会带来一些副作用,您需要注意这些副作用,例如,如果工作进程意外终止,则将还原任务,并在下一次恢复该工作进程时重新运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM