程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python multithreaded programming -04-threading module - event

編輯:Python

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

1. threading.Condition Introduce

1.1 threading.Event Mechanism

         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 .

1.2 threading.Event Properties and methods

threading.Event Properties and methods Serial number Properties and methods describe 1clear() eliminate Event Signal flags inside objects , It is set to False.2is_set()/isSet() Judge the status of internal signal signs . When using set() after ,isSet() Method returns True; When using clear() after ,isSet() Method returns False.3set() Set up Event The signal flags inside the object are True.4wait() This method only works when the internal signal is True Will be executed and returned . When the internal signal sign is False when , be wait() Wait until it is True Only return when .

2. threading.Condition Use demonstration

# 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 !

'''


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved