Python multithreading typical problem: Example of bus Algorithm for passengers, python Multithreading
This article provides an example of a typical Python multi-thread problem where a passenger runs a bus algorithm. We will share this with you for your reference. The details are as follows:
Problem description:
Passengers take bus problems, drivers, passengers, and conductor work together to simulate the work of the three through multiple threads.
Driver: Driving and parking
CONDUCTOR: Open the door and close the door
Passengers: Get on the bus, get off the bus
Use the Python Event for Synchronous thread CommunicationThe Code is as follows:
# *-* Coding: gb2312 *-* import threadingimport timestationName = ("Station 0", "station 1", "station 2", "Station 3", "station 4 ", "Station 5", "station 6") currentStationIndex =-1 eventBusStop = threading. event () eventClosedDoor = threading. event () eventOpenedDoor = threading. event () stationCount = len (stationName) class Passenger (threading. thread): def _ init _ (self, no, getonStation, getoffStation): self. no = no self. getonStation = getonStation self. getoffStation = getoffStation threading. thread. _ init _ (self) def run (self): bExit = False global currentStationIndex global stationCount bAlreadyGetOnStation = False while not bExit: eventOpenedDoor. wait () if self. getonStation = currentStationIndex and bAlreadyGetOnStation = False: print "Passenger % d boarding in % s" % (self. no, stationName [currentStationIndex]) bAlreadyGetOnStation = True elif self. getoffStation = currentStationIndex: print "passengers % d get off at % s" % (self. no, stationName [currentStationIndex]) bExit = True time. sleep (1) class Driver (threading. thread): def run (self): bExit = False global currentStationIndex global stationCount while not bExit: print "Driver: Start the bus ..... "time. sleep (5) currentStationIndex + = 1 print "Driver: arrival", stationName [currentStationIndex] eventBusStop. set () eventClosedDoor. wait () eventClosedDoor. clear () if currentStationIndex = stationCount-1: bExit = Trueclass Conductor (threading. thread): def run (self): bExit = False global currentStationIndex global stationCount while not bExit: eventBusStop. wait () eventBusStop. clear () print "conductor opens the door: % s to" % (stationName [currentStationIndex]) eventOpenedDoor. set () time. sleep (5) print "conductor closes the door" eventOpenedDoor. clear () eventClosedDoor. set () if currentStationIndex = stationCount-1: bExit = Truedef test (): passPool = [] passPool. append (passPool (0, 0, 3) passPool. append (passPool (1, 1, 3) passPool. append (passPool (2, 2, 4) passPool. append (passPool (3, 0, 5) passPool. append (passPool (4, 1, 3) passPool. append (passPool (5, 2, 4) passPool. append (passPool (6, 4, 5) passPool. append (passPool (7,0, 2) passPool. append (passPool (8, 1, 3) passPool. append (Conductor () passPool. append (Driver () leng = len (passPool) for I in range (leng): passPool [I]. start () if _ name __= = '_ main _': test ()
The output result is as follows: