Python multithreading typical problem: Example of bus Algorithm for passengers, python Multithreading

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.