예제 #1
0
#       a) Running the code repeatedly is a good way to test for correctness.
#       b) To make sure your code is correct, you must reason about the code
#           instead of rely on observed outputs.


class Worker1(MPthread):
    def __init__(self, mp):
        MPthread.__init__(self, mp, 'Worker 1')

    def run(self):
        while True:
            print('Hello from Worker 1')


class Worker2(MPthread):
    def __init__(self, mp):
        MPthread.__init__(self, mp, 'Worker 2')

    def run(self):
        while True:
            print('Hello from Worker 2')


if __name__ == '__main__':
    mp = MP()
    w1 = Worker1(mp)
    w2 = Worker2(mp)

    w1.start()
    w2.start()
예제 #2
0
                print('coordinator {} reserved cars {}, {}'.format(
                    self.id, first_rental, extra_rental))
            else:
                print('coordinator {} reserved car {}'.format(
                    self.id, first_rental))

            self.delay(0.1)

            if needs_extra:
                if extra_rental < first_rental:  # ordering
                    rental_cars[extra_rental].release()
                    rental_cars[first_rental].release()
                else:
                    rental_cars[first_rental].release()
                    rental_cars[extra_rental].release()
            else:
                rental_cars[first_rental].release()


################################################################################
## DO NOT WRITE OR MODIFY ANY CODE BELOW THIS LINE #############################
################################################################################

if __name__ == '__main__':

    CARS_IN_STOCK = 5
    manager = MP()
    rental_cars = [manager.Lock('m' + str(i)) for i in range(CARS_IN_STOCK)]
    for i in range(3):
        Coordinator(i, manager).start()