Event objects. : Synchronize : Thread PYTHON TUTORIALS


PYTHON TUTORIALS » Thread » Synchronize »

 

Event objects.


import threading
import random
import time

class VehicleThreadthreading.Thread ):
   def __init__self, threadName, event ):
      threading.Thread.__init__self, name = threadName )
      self.threadEvent = event
      
   def runself ):
      time.sleeprandom.randrange110 ) )
      print "%s arrived at %s" self.getName(), time.ctimetime.time() ) )

      self.threadEvent.wait()

      print "%s passes through intersection at %s" self.getName(), time.ctimetime.time() ) )

greenLight = threading.Event()
vehicleThreads = []

for i in range111 ):
   vehicleThreads.appendVehicleThread"Vehicle" + str),greenLight ) )

for vehicle in vehicleThreads:
   vehicle.start()

while threading.activeCount() 1:
   greenLight.clear()
   print "RED LIGHT! at", time.ctimetime.time() )
   time.sleep)

   print "GREEN LIGHT! at", time.ctimetime.time() )
   greenLight.set()
   time.sleep)



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Thread
» Synchronize