我需要定期调用一个任务,但(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)