如何在循环中并行运行一个函数,并在完成新参数后再次调用它?

时间:2019-05-13 09:43:53

标签: python multiprocessing

我正在尝试实现以下模式:

async def test(i): 
    time.sleep(10)
    print("Ready", i)

i = 0
while True:
    print("New iteration")
    #here I want to call test function asynchronously in a loop and go to the next iteration
    #if previous test execution is completed:
        asyncio.run(test(i))
    i += 1

预期输出为:

New iteration
New iteration
...
New iteration
Ready, 14235
New iteration
New iteration
...
New iteration
Ready, 29513
New iteration 
...

我有两个问题。首先,我不知道如何检查测试功能是否已经完成。第二个问题是,在完成test(i)之前似乎不会执行“ i + = 1”。我不想等

使用asyncio或多处理程序包能否实现我所需要的?

0 个答案:

没有答案