Skip to content

Instantly share code, notes, and snippets.

@kamalhm
Created April 17, 2018 07:46
Show Gist options
  • Save kamalhm/0d917d4628fddc2dad4c15d84863efbf to your computer and use it in GitHub Desktop.
Save kamalhm/0d917d4628fddc2dad4c15d84863efbf to your computer and use it in GitHub Desktop.
# Fig. 19.15: fig19_15.py
# Event objects.
import threading
import random
import time
class VehicleThread(threading.Thread):
"""Class representing a motor vehicle at an intersection"""
def __init__(self, threadName, event):
"""Initializes thread"""
threading.Thread.__init__(self, name=threadName)
# ensures that each vehicle waits for a green light
self.threadEvent = event
def run(self):
"""Vehicle waits unless/until light is green"""
# stagger arrival times
time.sleep(random.randrange(1, 10))
# prints arrival time of car at intersection
print("%s arrived at %s" %
(self.getName(), time.ctime(time.time()))
)
# flag is false until light is green
self.threadEvent.wait()
# displays time that car departs intersection
print("%s passes through intersection at %s" %
(self.getName(), time.ctime(time.time())))
greenLight = threading.Event()
vehicleThreads = []
# creates and starts ten Vehicle threads
for i in range(1, 11):
vehicleThreads.append(VehicleThread("Vehicle" + str(i),
greenLight))
for vehicle in vehicleThreads:
vehicle.start()
while threading.activeCount() > 1:
# sets the Event's flag to false -- block all incoming vehicles
greenLight.clear()
print("RED LIGHT! at", time.ctime(time.time()))
time.sleep(3)
# sets the Event's flag to true -- awaken all waiting vehicles
print("GREEN LIGHT! at", time.ctime(time.time()))
greenLight.set()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment