diff --git a/python-example/scripts/asyncio/README.md b/python-example/scripts/asyncio/README.md new file mode 100644 index 00000000..430a4c77 --- /dev/null +++ b/python-example/scripts/asyncio/README.md @@ -0,0 +1,81 @@ +# 非同期IO + +- https://docs.python.org/ja/3.12/library/asyncio.html + +`async def`でコルーチンを作成できる。 + +```bash +python -m scripts.asyncio.example1 +``` + +コルーチンは`await`で実行できる。 + +```bash +python -m scripts.asyncio.example2 +``` + +``` +21:24:28 start +21:24:30 hello +21:24:31 world +21:24:31 end +``` + +`asyncio.create_task()`はコルーチンをスケジュールできる。 + +```bash +python -m scripts.asyncio.example3 +``` + +``` +21:24:34 start +21:24:35 world +21:24:36 hello +21:24:36 end +``` + +`asyncio.gather()`で複数のコルーチンを束ねられる。 + +```bash +python -m scripts.asyncio.example4 +``` + +``` +21:29:58 start +21:30:00 21:30:00 hello +21:30:00 21:29:59 world +21:30:00 end +``` + +`asyncio.Lock`でロックできる。 + +```bash +python -m scripts.asyncio.example5 +``` + +``` +21:34:45 start +21:34:47 hello +21:34:48 world +21:34:48 end +``` + +セマフォ(`asyncio.Semaphore()`)もある。 + +```bash +python -m scripts.asyncio.example6 +``` + +``` +21:39:06 start +21:39:07 msg#1 +21:39:07 msg#2 +21:39:07 msg#3 +21:39:08 msg#4 +21:39:08 msg#5 +21:39:08 msg#6 +21:39:09 msg#7 +21:39:09 msg#8 +21:39:09 msg#9 +21:39:09 end +``` diff --git a/python-example/scripts/asyncio/example1.py b/python-example/scripts/asyncio/example1.py new file mode 100644 index 00000000..22116acc --- /dev/null +++ b/python-example/scripts/asyncio/example1.py @@ -0,0 +1,11 @@ +import asyncio +import time + + +async def main() -> None: + print(f"{time.strftime('%X')} hello") + await asyncio.sleep(1) + print(f"{time.strftime('%X')} world") + + +asyncio.run(main()) diff --git a/python-example/scripts/asyncio/example2.py b/python-example/scripts/asyncio/example2.py new file mode 100644 index 00000000..652ec435 --- /dev/null +++ b/python-example/scripts/asyncio/example2.py @@ -0,0 +1,19 @@ +import asyncio +import time + + +async def say(sleep: float, msg: str) -> None: + await asyncio.sleep(sleep) + print(f"{time.strftime('%X')} {msg}") + + +async def main() -> None: + print(f"{time.strftime('%X')} start") + said1 = say(2, "hello") + said2 = say(1, "world") + await said1 + await said2 + print(f"{time.strftime('%X')} end") + + +asyncio.run(main()) diff --git a/python-example/scripts/asyncio/example3.py b/python-example/scripts/asyncio/example3.py new file mode 100644 index 00000000..7f18db07 --- /dev/null +++ b/python-example/scripts/asyncio/example3.py @@ -0,0 +1,19 @@ +import asyncio +import time + + +async def say(sleep: float, msg: str) -> None: + await asyncio.sleep(sleep) + print(f"{time.strftime('%X')} {msg}") + + +async def main() -> None: + print(f"{time.strftime('%X')} start") + said1 = asyncio.create_task(say(2, "hello")) + said2 = asyncio.create_task(say(1, "world")) + await said1 + await said2 + print(f"{time.strftime('%X')} end") + + +asyncio.run(main()) diff --git a/python-example/scripts/asyncio/example4.py b/python-example/scripts/asyncio/example4.py new file mode 100644 index 00000000..7759c89f --- /dev/null +++ b/python-example/scripts/asyncio/example4.py @@ -0,0 +1,20 @@ +import asyncio +import time + + +async def say(sleep: float, msg: str) -> str: + await asyncio.sleep(sleep) + return f"{time.strftime('%X')} {msg}" + + +async def main() -> None: + print(f"{time.strftime('%X')} start") + said1 = say(2, "hello") + said2 = say(1, "world") + msg1, msg2 = await asyncio.gather(said1, said2) + print(f"{time.strftime('%X')} {msg1}") + print(f"{time.strftime('%X')} {msg2}") + print(f"{time.strftime('%X')} end") + + +asyncio.run(main()) diff --git a/python-example/scripts/asyncio/example5.py b/python-example/scripts/asyncio/example5.py new file mode 100644 index 00000000..c3142bfb --- /dev/null +++ b/python-example/scripts/asyncio/example5.py @@ -0,0 +1,21 @@ +import asyncio +import time + + +async def say(lock: asyncio.Lock, sleep: float, msg: str) -> None: + async with lock: + await asyncio.sleep(sleep) + print(f"{time.strftime('%X')} {msg}") + + +async def main() -> None: + print(f"{time.strftime('%X')} start") + lock = asyncio.Lock() + said1 = asyncio.create_task(say(lock, 2, "hello")) + said2 = asyncio.create_task(say(lock, 1, "world")) + await said1 + await said2 + print(f"{time.strftime('%X')} end") + + +asyncio.run(main()) diff --git a/python-example/scripts/asyncio/example6.py b/python-example/scripts/asyncio/example6.py new file mode 100644 index 00000000..53df0a96 --- /dev/null +++ b/python-example/scripts/asyncio/example6.py @@ -0,0 +1,19 @@ +import asyncio +import time + + +async def say(semaphore: asyncio.Semaphore, msg: str) -> None: + async with semaphore: + await asyncio.sleep(1) + print(f"{time.strftime('%X')} {msg}") + + +async def main() -> None: + print(f"{time.strftime('%X')} start") + semaphore = asyncio.Semaphore(3) + tasks = [say(semaphore, f"msg#{i}") for i in range(1, 10)] + await asyncio.gather(*tasks) + print(f"{time.strftime('%X')} end") + + +asyncio.run(main())