forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_3.py
More file actions
45 lines (35 loc) · 951 Bytes
/
Copy pathexample_3.py
File metadata and controls
45 lines (35 loc) · 951 Bytes
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
import time
import queue
from lib.elapsed_time import ET
def task(name, queue):
while not queue.empty():
delay = queue.get()
et = ET()
print(f"Task {name} running")
time.sleep(delay)
print(f"Task {name} total elapsed time: {et():.1f}")
yield
def main():
"""
This is the main entry point for the program
"""
# Create the queue of 'work'
work_queue = queue.Queue()
# Put some 'work' in the queue
for work in [15, 10, 5, 2]:
work_queue.put(work)
tasks = [task("One", work_queue), task("Two", work_queue)]
# Run the tasks
et = ET()
done = False
while not done:
for t in tasks:
try:
next(t)
except StopIteration:
tasks.remove(t)
if len(tasks) == 0:
done = True
print(f"\nTotal elapsed time: {et():.1f}")
if __name__ == "__main__":
main()