如何在Celery中使两个任务互斥? [英] How to make two tasks mutually exclusive in Celery?

查看:82
本文介绍了如何在Celery中使两个任务互斥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以禁止在Celery中同时运行两个不同的任务?我当时正在考虑定义一个新的并发级别为1的队列,并将这些任务发送到该队列,但是我找不到一个示例.有可能吗?

Is there a way to disallow two different tasks to run simultaneously in Celery? I was thinking about defining a new queue with concurrency level=1, and send those tasks to that queue, but I couldn't find an example. Is that possible?

谢谢!

推荐答案

是的,如果您不必担心整体吞吐量,则可以创建一个单独的队列并将专用的并发设置为1.可以根据需要创建任意数量的队列,并配置每个工作人员从哪个队列接收消息.

Yes, if you don't need to worry about overall throughput it is possible to create a separate queue and have a dedicated worker with concurrency set to 1. You can create as many queues as you want and configure which of those queues each worker receives messages from.

启动工作程序时,可以传递 -Q 参数设置其队列,并传递 -c 参数设置其使用的线程数,如队列

When starting the worker, you can pass the -Q parameter to set its queues and the -c parameter to set how many threads it uses, as discussed in the Queues and Concurrency sections of the Workers Guide.

celery-一个my_project worker -l info -Q queue1 -c 1

然后,您可以使用路由指南.

CELERY_ROUTES = {'my_app.tasks.task1':{'queue':'queue1'},'my_app.tasks.task2':{'queue':'queue2'},}

或者,您可以在提交每个任务实例时根据呼叫任务指南.

Alternatively, you can specify the queue at the time you submit each task instance based on the Calling Tasks Guide.

task1.apply_async(queue ='queue1')

这篇关于如何在Celery中使两个任务互斥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆