Skip to content

Commit

Permalink
非同期IOを学ぶ
Browse files Browse the repository at this point in the history
  • Loading branch information
backpaper0 committed Jul 24, 2024
1 parent d6a92b0 commit 7ce329e
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 0 deletions.
81 changes: 81 additions & 0 deletions python-example/scripts/asyncio/README.md
Original file line number Diff line number Diff line change
@@ -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
```
11 changes: 11 additions & 0 deletions python-example/scripts/asyncio/example1.py
Original file line number Diff line number Diff line change
@@ -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())
19 changes: 19 additions & 0 deletions python-example/scripts/asyncio/example2.py
Original file line number Diff line number Diff line change
@@ -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())
19 changes: 19 additions & 0 deletions python-example/scripts/asyncio/example3.py
Original file line number Diff line number Diff line change
@@ -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())
20 changes: 20 additions & 0 deletions python-example/scripts/asyncio/example4.py
Original file line number Diff line number Diff line change
@@ -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())
21 changes: 21 additions & 0 deletions python-example/scripts/asyncio/example5.py
Original file line number Diff line number Diff line change
@@ -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())
19 changes: 19 additions & 0 deletions python-example/scripts/asyncio/example6.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 7ce329e

Please sign in to comment.