简体   繁体   English

如何推迟 Celery 任务的执行?

[英]How can I defer the execution of Celery tasks?

I have a small script that enqueues tasks for processing.我有一个小脚本,可以将任务排入队列以进行处理。 This script makes a whole lot of database queries to get the items that should be enqueued.此脚本进行大量数据库查询以获取应入队的项目。 The issue I'm facing is that the celery workers begin picking up the tasks as soon as it is enqueued by the script.我面临的问题是 celery 工作人员在脚本排队后立即开始接收任务。 This is correct and it is the way celery is supposed to work but this often leads to deadlocks between my script and the celery workers.这是正确的,也是 celery 应该工作的方式,但这通常会导致我的脚本和 celery 工作人员之间出现僵局。

Is there a way I could enqueue all my tasks from the script but delay execution until the script has completed or until a fixed time delay?有没有办法可以将脚本中的所有任务排入队列,但将执行延迟到脚本完成或固定时间延迟?

I couldn't find this in the documentation of celery or django-celery.我在 celery 或 django-celery 的文档中找不到这个。 Is this possible?这可能吗?

Currently as a quick-fix I've thought of adding all the items to be processed into a list and when my script is done executing all the queries, I can simply iterate over the list and enqueue the tasks.目前作为一个快速修复,我想将所有要处理的项目添加到列表中,当我的脚本执行完所有查询时,我可以简单地遍历列表并将任务排入队列。 Maybe this would resolve the issue but when you have thousands of items to enqueue, this might be a bad idea.也许这可以解决问题,但是当您有数千个项目要排队时,这可能是一个坏主意。

eta/countdown options enable to delay the task execution: eta/countdown 选项可以延迟任务执行:

http://docs.celeryproject.org/en/master/userguide/calling.html#eta-and-countdown http://docs.celeryproject.org/en/master/userguide/calling.html#eta-and-countdown

I think you are trying to avoid race condition of your own scripts, not asking for a method to delay a task run.我认为您正在尝试避免自己脚本的竞争条件,而不是要求延迟任务运行的方法。

Then you can create a task, and in that task, call each of your task with .apply(), not .apply_async() or .delay().然后您可以创建一个任务,并在该任务中使用 .apply() 而不是 .apply_async() 或 .delay() 调用您的每个任务。 So that these tasks run sequentially以便这些任务依次运行

To define delay on task execution use apply_async() with countdown option in the following format:要定义任务执行的延迟,请使用apply_async()和以下格式的倒计时选项:

from datetime import timedelta
#Delay for 10 seconds
T.apply_async(args=[arg1, arg2], countdown = 10)
#Delay for 10 days 
T.apply_async(args=[arg1, arg2], countdown = timedelta(days=10))

By using timedelta it is possible to define more complicated delays for task execution.通过使用timedelta ,可以为任务执行定义更复杂的延迟。 Note that delay() is another calling API but does not support countdown for delay.请注意, delay()是另一个调用 API,但不支持延迟倒计时。

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

相关问题 如何捕获在执行一系列Celery任务期间生成的所有python日志记录? - How can I capture all of the python log records generated during the execution of a series of Celery tasks? 如何在django应用程序中手动触发/启动芹菜任务? - How can I manually trigger/start celery tasks in a django application? 我如何使用 python 协程作为 celery 任务 - How can i use python coroutines as celery tasks 如何从 Python Celery 中的另一个任务触发任务? - How can I trigger tasks from another task in Python Celery? 如何使用Celery守护程序自动重新加载任务模块? - How can I automatically reload tasks modules with Celery daemon? 如何在不执行 Celery 的情况下将任务发送到特定队列? - How can I send tasks to specific queues without execute in Celery? 如何禁用 celery(使用 redis)中的任务重新交付? - How can I disable redelivery of tasks in celery (with redis)? 芹菜花-如何加载以前捕获的任务? - Celery Flower - how can i load previous catched tasks? 如何使用Celery制作包含所有待处理任务的仪表板? - How can I make a dashboard with all pending tasks using Celery? 如何告诉芹菜工作者停止接受任务? 如何检查芹菜工作者任务是否正在运行? - How do I tell celery worker to stop accepting tasks? How can I check if any celery worker tasks are running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM