如何并行运行多个芹菜任务?

2024-05-15 05:19:59 发布

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

我有两个任务。在

@app.task
def run1():
    while True:
        print('run1')
        time.sleep(5)
    return


@app.task
def run2():
    while True:
        print('run2')
        time.sleep(2)
    return

如何从同一个控制台,从一个命令(最好使用不同数量的workers)同时运行这两个任务。在


Tags: 命令trueapptask数量returntimedef
1条回答
网友
1楼 · 发布于 2024-05-15 05:19:59

您需要使用^{}

The group primitive is a signature that takes a list of tasks that should be applied in parallel.

django shell的示例:

>>> from celery import group
>>> from myapp.tasks import run1, run2
>>>
>>> run_group = group(run1.s(), run2.s())
>>> run_group()
<GroupResult: 06b3e88b-6c10-4ba5-bb32-5005c82eedfe [cc734fbd-3531-45d1-8575-64f4eff35523, 
1075e822-a6e2-4c34-8038-369613ff687d]>

有关更复杂的用法,请参阅group上的文档。在

相关问题 更多 >

    热门问题