python asyncio

카테고리 없음 2020. 12. 11. 09:46

asyncio.gather 를 사용하자

- java completion executor 와 같은 동시성 제공.., 결과 끝날때까지 기다림, 코루틴 결과 취합가능함

 

 

 

 

import asyncio
import time

async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
return delay

async def main():
print(f"started at {time.strftime('%X')}")

# task1 = asyncio.create_task(
# say_after(1, 'hello'))
#
# task2 = asyncio.create_task(
# say_after(2, 'world'))

t = await asyncio.gather(
say_after(1,'hello')
,say_after(2, 'world')

)

print(t)
# Wait until both tasks are completed (should take
# around 2 seconds.)
# await task1
# await task2

print(f"finished at {time.strftime('%X')}")


asyncio.run(main())

Posted by agaytr
,