Objective record
1. threading.Condition Introduce
1.1 threading.Event Mechanism
1.2 threading.Event Properties and methods
2. threading.Condition Use demonstration
Python Multithreaded programming Directory
Python Multithreaded programming -01-threading The module first
Python Multithreaded programming -02-threading modular - Use of locks
Python Multithreaded programming -03-threading modular - Condition
stay Python Multithreaded programming -03-threading modular - Condition Producers are introduced in - Code implementation of the consumer pattern , Used threading.Condition To control the use of the same resource pool , The producer thread and the consumer thread are equivalent , There is no master-slave . and threading.Event The mechanism is similar to the pattern of one thread giving orders to other threads , Other threads will hold a threading.Event The object of , These threads will wait for this event “ happen ”, If this never happens , Then these threads will block , Until the end of the event “ happen ”. And this kind of scene is very common .
# Design a red street lamp ,threading.Event It is the direction of the car
# stay 10 In seconds ,4 Seconds ago, it was a red light ,4 Seconds to 8 Seconds is green , There is a red light behind
# Design a car class , Traffic lights in the direction of the car , A green light (set True) When to pass , A red light (set False) wait for
# Waiting for the most 10 second , Then turn around ; among timer It is used to indicate the second when it comes to the intersection of red street lights
# Design a walking human , Traffic lights in the direction of the car , A green light (set True) Time to wait , A red light (set False) Current
# Waiting for the most 10 second , Then turn around ; among timer It is used to indicate the second when it comes to the intersection of red street lights
%%time
import threading
import time
# Design a red street lamp ,threading.Event It is the direction of the car
# stay 10 In seconds ,4 Seconds ago, it was a red light ,4 Seconds to 8 Seconds is green , There is a red light behind
# Design a car class , Traffic lights in the direction of the car , A green light (set True) When to pass , A red light (set False) wait for
# Waiting for the most 10 second , Then turn around ; among timer It is used to indicate the second when it comes to the intersection of red street lights
class Car(threading.Thread):
def __init__(self,color,event,timer):
super().__init__()
self.color=color
self.event=event
self.timer=timer
def run(self):
time.sleep(self.timer)
count=0
while(count<11):
if(self.event.is_set()):
print(time.ctime(),"The car of {0} will thransfer ".format(self.color))
time.sleep(1)
break
else:
print(time.ctime(),"The car of {0} is waiting! ".format(self.color))
self.event.wait(10)
count+=1
if(count>=11):
print(time.ctime(),"The car of {0} is gone! ".format(self.color))
# Design a walking human , Traffic lights in the direction of the car , A green light (set True) Time to wait , A red light (set False) Current
# Waiting for the most 10 second , Then turn around ; among timer It is used to indicate the second when it comes to the intersection of red street lights
class Man(threading.Thread):
def __init__(self,name,event,timer):
super().__init__()
self.name=name
self.event=event
self.timer=timer
def run(self):
time.sleep(self.timer)
count=0
while(count<11):
if(not self.event.is_set()):
print(time.ctime(),"The man {0} will thransfer ".format(self.name))
break
else:
print(time.ctime(),"The man {0} is waiting! ".format(self.name))
time.sleep(1)
count+=1
if(count>=11):
print(time.ctime(),"The man {0} is gone! ".format(self.name))
if __name__=="__main__":
light=threading.Event()
ada=Man("ada",light,1)
ivy=Man("ivy",light,5)
allen=Man("allen",light,8)
jessica=Man("jessica",light,9)
bluecar=Car("blue",light,2)
yellowcar=Car("yellow",light,7)
whitecar=Car("white",light,9)
count=0
ada.start()
ivy.start()
allen.start()
jessica.start()
bluecar.start()
yellowcar.start()
whitecar.start()
while(count<10):
if(count==4):
light.set()
if(count==8):
light.clear()
if(light.is_set()):
print("{0} the light is {1}".format(time.ctime(),"Green"))
else:
print("{0} the light is {1}".format(time.ctime(),"Red"))
time.sleep(1)
count+=1
The operation results are as follows :
'''
If everyone thinks it's ok , Please order a praise or collection , Think of a blog to increase popularity , Thank you very much !
'''
2021 year 9 month Python Analy