-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers_trio.py
63 lines (48 loc) · 1.61 KB
/
workers_trio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import trio
import random
import sys
from easy_timing import timer
MAX_TASKS = 100
MAX_WORKERS = 10
task_completed = 0
task_queued = 0
def print_ident(string, ident):
print(ident.count(".") * " " + string)
async def task(nursery, task_id, lock, task_status=trio.TASK_STATUS_IGNORED):
global task_completed, task_queued
task_status.started()
async with lock:
print_ident(f"Begin task #{task_id}", task_id)
await trio.sleep(0.5 + random.random())
task_completed += 1
print_ident(
f"End task #{task_id} ({task_queued - task_completed} in queue, {task_completed} completed)",
task_id,
)
if task_queued < MAX_TASKS:
await nursery.start(task, nursery, task_id + ".1", lock)
task_queued += 1
if task_queued < MAX_TASKS - 1:
await nursery.start(task, nursery, task_id + ".2", lock)
task_queued += 1
if task_queued < MAX_TASKS - 2:
await nursery.start(task, nursery, task_id + ".3", lock)
task_queued += 1
async def main():
global task_queued
print("Begin parent")
with trio.move_on_after(20):
async with trio.open_nursery() as nursery:
lock = trio.CapacityLimiter(MAX_WORKERS)
print("Begin nursery")
await nursery.start(task, nursery, "1", lock)
task_queued += 1
print("Waiting for children")
print("End parent")
if __name__ == "__main__":
try:
with timer(""):
trio.run(main)
except KeyboardInterrupt:
print("\n Bye bye.")
sys.exit(0)