小编Maj*_*rin的帖子

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

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

在下面的代码中,如何do_something()在不需要await结果的情况下运行任务?

 import asyncio
 import time
 from random import randint

 period = 1  # Second


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


 async def do_something(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 …
Run Code Online (Sandbox Code Playgroud)

python asynchronous python-3.x python-asyncio

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

asynchronous ×1

python ×1

python-3.x ×1

python-asyncio ×1