如何在Celery中使两个任务互斥?

编程入门 行业动态 更新时间:2024-10-26 12:27:03
本文介绍了如何在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中使两个任务互斥?

本文发布于:2023-11-24 04:12:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   互斥   如何在   Celery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!