2024-heraklion-parallel-python/extras/kitchen_asyncio/kitchen_serial.py

84 lines
1.9 KiB
Python
Raw Normal View History

2024-08-29 18:21:59 +02:00
import argparse
import random
import time
def do_work(duration, result):
t = random.uniform(duration/5 * 0.5, duration/5 * 1.5)
time.sleep(t)
return result
def fetch_olive_oil():
print('Fetching olive oil…')
return do_work(5, 'oil')
def fetch_dako_rusk():
print('Fetching dako rusk…')
return do_work(5, 'rusk')
def fetch_tomato():
print('Fetching tomato…')
return do_work(3, 'tomato')
def fetch_feta():
print('Fetching feta…')
return do_work(3, 'feta')
def chop_tomato(tomato):
assert tomato == 'tomato'
print('Chopping tomato…')
return do_work(1, f'chopped {tomato}')
def water_rusk(rusk):
assert rusk == 'rusk'
print('Watering rusk…')
return do_work(0.2, f'wet {rusk}')
def oil_rusk(rusk, oil):
assert rusk == 'wet rusk'
assert oil == 'oil'
print(f'Pouring {oil} on {rusk}')
return do_work(0.5, f'{rusk} with {oil}')
def decorate_rusk(rusk, *toppings):
result = rusk
for topping in toppings:
print(f'Putting {topping} on {result}')
result = do_work(1, f'{result} with {topping}')
return result
def store_dako(dako):
print(f'Storing {dako}')
do_work(1, None)
def prepare_dako():
print('Making dako…')
oil = fetch_olive_oil()
rusk = fetch_dako_rusk()
tomato = fetch_tomato()
tomato = chop_tomato(tomato)
rusk = water_rusk(rusk)
rusk = oil_rusk(rusk, oil)
feta = fetch_feta()
dako = decorate_rusk(rusk, tomato, feta)
store_dako(dako)
print(f'{dako} is ready!')
def prepare_dakos(n_dakos):
for n in range(args.n_dakos):
prepare_dako()
def parse_args():
p = argparse.ArgumentParser()
p.add_argument(
'n_dakos',
type=int,
nargs='?',
default=1,
help='How many dakos to make?',
)
return p.parse_args()
if __name__ == '__main__':
args = parse_args()
prepare_dakos(args.n_dakos)