시행착오/[python]

asyncio 예제 코드

bled 2021. 2. 16. 17:57

coro1 함수는 3초마다 1씩 증가하는 값 출력하는 함수

coro2 함수는 input 값이 들어오면 입력한 값 출력하는 함수

import asyncio as aio

async def coro1():
    i = 1
    while True:
        print(i)
        i = i+1
        await aio.sleep(3)

async def coro2(loop):
    while True:
        msg = await loop.run_in_executor(None, input, ": ")
        print('->', msg)


async def main():
    loop = aio.get_event_loop()
    task1 = loop.create_task(coro1())
    task2 = loop.create_task(coro2(loop))

    await task1
    await task2

aio.run(main())