如何在不等待的情况下运行Asyncio任务?

时间:2019-05-27 01:25:52

标签: python python-3.x python-asyncio

我需要定期调用任务,但是(a)等待时间几乎超过了周期。

在下面的代码中,如何运行do_somthing()任务而无需等待结果?

 import asyncio
 import time
 from random import randint

 period = 1  # Second


 def get_epoch_ms():
     return int(time.time() * 1000.0)


 async def do_somthing(name):
     print("Start  :", name, get_epoch_ms())
     try:
         # Do something which may takes more than 1 secs.
         slp = randint(1, 5)
         print("Sleep  :", name, get_epoch_ms(), slp)
         await asyncio.sleep(slp)
     except Exception as e:
         print("Error  :", e)

     print("Finish :", name, get_epoch_ms())


 async def main():
     i = 0
     while True:
         i += 1
         # Todo : this line should be change
         await do_somthing('T' + str(i))
         await asyncio.sleep(period)


 asyncio.get_event_loop().run_until_complete(main())

2 个答案:

答案 0 :(得分:0)

调用asyncio.create_task而不是await创建协程,以生成在后台运行的任务对象。在下一次迭代时,您可以检查任务是否完成,并相应地等待/取消。 (否则,asyncio将抱怨未完成的任务被垃圾回收。)

答案 1 :(得分:0)

您的问题是使用run_until_complete(main())不能满足您的并发目的,因此假设您的协同程序任务(do_somthing())绑定到5,您的代码将如下所示:

import time
from random import randint

period = 1  # Second

def get_epoch_ms():
    return int(time.time() * 1000.0)

async def do_somthing(name):
    print("Start  :", name, get_epoch_ms())
    try:
        # Do something which may takes more than 1 secs.
        slp = randint(1, 5)
        print("Sleep  :", name, get_epoch_ms(), slp)
        await asyncio.sleep(slp)
    except Exception as e:
        print("Error  :", e)

    print("Finish :", name, get_epoch_ms())

loop = asyncio.get_event_loop()
futures = [loop.create_task(do_somthing('T' + str(i)))
           for i in range(5)]

loop.run_forever()

for f in futures:
    f.cancel()

这是其输出中的并发工作流:

Start  : T0 1558937750705
Sleep  : T0 1558937750705 5
Start  : T1 1558937750705
Sleep  : T1 1558937750705 1
Start  : T2 1558937750705
Sleep  : T2 1558937750705 4
Start  : T3 1558937750705
Sleep  : T3 1558937750705 5
Start  : T4 1558937750705
Sleep  : T4 1558937750705 5
Finish : T1 1558937751707
Finish : T2 1558937754709
Finish : T0 1558937755707
Finish : T3 1558937755708
Finish : T4 1558937755708

但是,如果协程任务不受限制,则可以执行以下操作:

...
async def main(loop):
    i = 0
    while True:
        i += 1
        loop.create_task(do_somthing('T' + str(i)))
        await asyncio.sleep(period)


loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))