import asyncio
import time
async def say_after(delay, what):
print(f"Req time at {time.strftime('%X')} for "+ what)
await asyncio.sleep(delay)
# print(what)
print(f"Return time at {time.strftime('%X')} for "+ what + " delay = "+ str(delay))
return ("hero , delay = " + str(delay)+ " value = "+ what )
async def main():
# task1 = asyncio.create_task(
# say_after(1, 'hello'))
#
# task2 = asyncio.create_task(
# say_after(2, 'world'))
print(f"started at {time.strftime('%X')}")
tasks = []
loop: AbstractEventLoop = asyncio.get_event_loop()
for i in range(5):
tasks.append(asyncio.create_task(
say_after(i+1, 'pila')
))
# print( val + "")
for task in tasks:
# print( await task)
print(await task)
# Wait until both tasks are completed (should take
# around 2 seconds.)
# print(await task1)
# print(await task2)
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
import time
async def say_after(delay, what):
print(f"Req time at {time.strftime('%X')} for "+ what)
await asyncio.sleep(delay)
# print(what)
print(f"Return time at {time.strftime('%X')} for "+ what + " delay = "+ str(delay))
return ("hero , delay = " + str(delay)+ " value = "+ what )
async def main():
# task1 = asyncio.create_task(
# say_after(1, 'hello'))
#
# task2 = asyncio.create_task(
# say_after(2, 'world'))
print(f"started at {time.strftime('%X')}")
tasks = []
loop: AbstractEventLoop = asyncio.get_event_loop()
for i in range(5):
tasks.append(asyncio.create_task(
say_after(i+1, 'pila')
))
# print( val + "")
for task in tasks:
# print( await task)
print(await task)
# Wait until both tasks are completed (should take
# around 2 seconds.)
# print(await task1)
# print(await task2)
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
Comments
Post a Comment