I need to call a task periodically but (a)waiting times are almost more than the period.
In the following code, How can I run do_something()
task without need to await
for the result?
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 line should be change
await do_something('T' + str(i))
await asyncio.sleep(period)
asyncio.get_event_loop().run_until_complete(main())