python-mastery/Solutions/8_5/multitask.py

34 lines
577 B
Python
Raw Normal View History

2023-07-17 03:21:00 +02:00
# multitask.py
from collections import deque
tasks = deque()
def run():
while tasks:
task = tasks.popleft()
try:
next(task)
tasks.append(task)
except StopIteration:
print('Task done')
def countdown(n):
while n > 0:
print('T-minus', n)
yield
n -= 1
def countup(n):
x = 0
while x < n:
print('Up we go', x)
yield
x += 1
if __name__ == '__main__':
tasks.append(countdown(10))
tasks.append(countdown(5))
tasks.append(countup(20))
run()