示例#1
0
def barber(door, printer):
    import random
    timer = Timer()
    while True:
        printer.write('Barber is sleeping.')
        customer = door.read()
        print_c.write('The barber has woken to give {0} a shave.'.format(customer))
        timer.sleep(random.random() * 5)
示例#2
0
def sendAlt(cout, num):
    """
    readset =
    writeset = cout
    """
    t = Timer()
    t.sleep(1)
    cout.write(num)
    return
示例#3
0
def barber(door, printer):
    import random
    timer = Timer()
    while True:
        printer.write('Barber is sleeping.')
        customer = door.read()
        print_c.write(
            'The barber has woken to give {0} a shave.'.format(customer))
        timer.sleep(random.random() * 5)
示例#4
0
def sendAlt(cout, num):
    """
    readset =
    writeset = cout
    """
    t = Timer()
    t.sleep(1)
    cout.write(num)
    return
示例#5
0
def shop(numberOfWaitingSeats, fromWorld, toBarber, fromBarber, toWorld):
    seats = []
    customersTrimmed = 0
    customersTurnedAway = 0
    closing = False
    while True:
        try:
            customer = fromBarber | fromWorld
            if isinstance(customer, Customer):
                if len(seats) <= numberOfWaitingSeats:
                    seats.append(customer)
                    print('Shop: Customer ' + str(customer.id) + ' takes a seat. ' + str(len(seats)) + ' in use.')
                    toBarber.write(customer)
                else:
                    customersTurnedAway += 1
                    print('Shop: Customer ' + str(customer.id) + ' turned away.')
                    toWorld.write(customer)
            elif isinstance(customer, SuccessfulCustomer):
                seatsTaken -= 1
                customersTrimmed += 1
                print('Shop: Customer ' + str(customer.c.id) + ' leaving trimmed.')
                toWorld.write(customer)
            else:
                raise ValueError('Shop: select failed, got a ' + str(customer))
            if len(seats) > 0:
                alt = Alt(toBarber, Timer().sleep(0.1))
                toBarber.write(seats[0])
                del(seats[0])
            else:
                if closing:
                    toBarber.poison()
                    print('Shop: Closing -- ' + str(customersTrimmed) + ' trimmed, and ' + str(customersTurnedAway) + ' turned away.')
                    toWorld.poison()
                    break
        except ChannelPoison:
            closing = True
示例#6
0
def hello():
    t = Timer()
    for i in range(5):
        print i
        t.sleep(1)