def shopIn(numberOfWaitingSeats, fromWorld, toBarber, toAccounts): seats = [] def trySendingToBarber(customer): channel, message = AltSelect(OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: del customer try: while True: customer = fromWorld() assert isinstance(customer, Customer) if len(seats) < numberOfWaitingSeats: seats.append(customer) print('Shop: Customer ' + str(customer.id) + ' takes a seat. ' + str(len(seats)) + ' in use.') channel, message = AltSelect(OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: seats = seats[1:] else: if len(seats) > 0: channel, message = AltSelect(OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: seats = seats[1:] print('Shop: Customer ' + str(customer.id) + ' turned away.') toAccounts(customer) except ChannelPoisonException: while(len(seats) > 0): toBarber(seats[0]) seats = seats[1:] poison(toBarber)
def shop(numberOfWaitingSeats, fromWorld, toBarber, fromBarber, toWorld): seats = [] customersTrimmed = 0 customersTurnedAway = 0 closing = False while True: try: if closing: channel, customer = fromBarber, fromBarber() else: # Order of channels is important here as it defines the priority of the channels. channel, customer = AltSelect(InputGuard(fromBarber), InputGuard(fromWorld)) if channel == fromWorld: assert isinstance(customer, Customer) if len(seats) <= numberOfWaitingSeats: seats.append(customer) print('Shop: Customer ' + str(customer.id) + ' takes a seat. ' + str(len(seats)) + ' in use.') else: print('Shop: Customer ' + str(customer.id) + ' turned away.') customersTurnedAway += 1 toWorld(customer) elif channel == fromBarber: assert isinstance(customer, SuccessfulCustomer) print('Shop: Customer ' + str(customer.c.id) + ' leaving trimmed.') customersTrimmed += 1 toWorld(customer) else: raise ValueError('Shop: AltSelect failed.') if len(seats) > 0: channel, message = AltSelect( OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: del (seats[0]) else: if closing: poison(toBarber) print('Shop: Closing -- ' + str(customersTrimmed) + ' trimmed, and ' + str(customersTurnedAway) + ' turned away.') poison(toWorld) break except ChannelPoisonException: print('Shop: Beginning the closing sequence.') closing = True
def shop(numberOfWaitingSeats, fromWorld, toBarber, fromBarber, toWorld): seats = [] customersTrimmed = 0 customersTurnedAway = 0 closing = False while True: try: if closing: channel, customer = fromBarber, fromBarber() else: # Order of channels is important here as it defines the priority of the channels. channel, customer = AltSelect(InputGuard(fromBarber), InputGuard(fromWorld)) if channel == fromWorld: assert isinstance(customer, Customer) if len(seats) <= numberOfWaitingSeats: seats.append(customer) print('Shop: Customer ' + str(customer.id) + ' takes a seat. ' + str(len(seats)) + ' in use.') else: print('Shop: Customer ' + str(customer.id) + ' turned away.') customersTurnedAway += 1 toWorld(customer) elif channel == fromBarber: assert isinstance(customer, SuccessfulCustomer) print('Shop: Customer ' + str(customer.c.id) + ' leaving trimmed.') customersTrimmed += 1 toWorld(customer) else: raise ValueError('Shop: AltSelect failed.') if len(seats) > 0: channel, message = AltSelect(OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: del(seats[0]) else: if closing: poison(toBarber) print('Shop: Closing -- ' + str(customersTrimmed) + ' trimmed, and ' + str(customersTurnedAway) + ' turned away.') poison(toWorld) break except ChannelPoisonException: print('Shop: Beginning the closing sequence.') closing = True
def shopIn(numberOfWaitingSeats, fromWorld, toBarber, toAccounts): seats = [] def trySendingToBarber(customer): channel, message = AltSelect(OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: del customer try: while True: customer = fromWorld() assert isinstance(customer, Customer) if len(seats) < numberOfWaitingSeats: seats.append(customer) print('Shop: Customer ' + str(customer.id) + ' takes a seat. ' + str(len(seats)) + ' in use.') channel, message = AltSelect( OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: seats = seats[1:] else: if len(seats) > 0: channel, message = AltSelect( OutputGuard(toBarber, msg=seats[0]), TimeoutGuard(seconds=0.1)) if channel == toBarber: seats = seats[1:] print('Shop: Customer ' + str(customer.id) + ' turned away.') toAccounts(customer) except ChannelPoisonException: while (len(seats) > 0): toBarber(seats[0]) seats = seats[1:] poison(toBarber)
def worldSource(numberOfCustomers, nextCustomerWaitTime, toShop): for i in range(numberOfCustomers): time.sleep(nextCustomerWaitTime()) print('World: Customer ' + str(i) + ' enters the shop.') toShop(Customer(i)) poison(toShop)
def world(numberOfCustomers, nextCustomerWaitTime, toShopIn): for i in range(numberOfCustomers): time.sleep(nextCustomerWaitTime()) toShopIn(Customer(i)) poison(toShopIn)