ところでcreate_subprocess_execのAPIがPopenと違うのなんとかならないですかね
async def a(s: str):
time.sleep(3)
print(s)
async def b(s: str):
await asyncio.sleep(3)
print(s)
async def c(s: str):
cmd = ["sleep", "3"]
proc = subprocess.Popen(args=cmd)
proc.wait()
print(s)
async def d(s: str):
cmd = ["sleep", "3"]
proc = await asyncio.create_subprocess_exec(cmd[0], *cmd[1:])
await proc.wait()
print(s)
async def main():
await asyncio.gather(a("1"), b("2"), b("3"))
await asyncio.gather(c("4"), d("5"), d("6"))
asyncio.run(main())
これ
> I/O(入出力)の待ち時間に別の処理をやらせることで 並行処理 をさせるための仕組み
Pythonで非同期処理をやる
https://zenn.dev/kevinrobot34/articles/python-asyncio-memo#asyncio
async関数の中でブロックしてしまうと、他のasync関数の実行を妨げてしまう(awaitしないと処理時間を譲れない)
async def a(s: str):
time.sleep(3)
print(s)
async def b(s: str):
await asyncio.sleep(3)
print(s)
async def main():
await asyncio.gather(a("a"), b("b"), b("c"))
asyncio.run(main())