-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6a92b0
commit 7ce329e
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |