Python Coroutine
Python の Coroutine(コルーチン)は,非同期処理や協調的な並行処理を実現するための仕組み 簡単に言うと「途中で処理を一時停止し,後で再開できる関数」
>>> async def sleeping(sec):... loop = asyncio.get_event_loop()... print(f'start: {sec}秒待つよ')... await loop.run_in_executor(None, time.sleep, sec)... print(f'finish: {sec}秒待つよ')... return sec...>>> async def m():... return await asyncio.gather(... sleeping(5),... sleeping(1),... sleeping(8),... sleeping(3),... sleeping(4)... )...>>> asyncio.run(m())start: 5秒待つよstart: 1秒待つよstart: 8秒待つよstart: 3秒待つよstart: 4秒待つよfinish: 1秒待つよfinish: 3秒待つよfinish: 4秒待つよfinish: 5秒待つよfinish: 8秒待つよ[5, 1, 8, 3, 4]