Python multithreaded programming - 04 threading module - Event

Catalogue

1. threading.Condition introduction

1.1 threading.Event mechanism

1.2 threading.Event properties and methods

2. threading.Condition usage demonstration

Python multithreaded programming directory

Python multithreaded programming-01-threading module

Python multithreaded programming - 02 threading module - use of locks

Python multithreaded programming - 03 threading module - Condition

1. threading.Condition introduction

1.1 threading.Event mechanism

In Python multithreading programming - 03 threading module - condition, this paper introduces the code implementation of producer consumer mode, using threading Condition to control the use of the same resource pool. The producer thread and consumer thread are equal, and there is no master-slave distinction. And threading The event mechanism is similar to the mode that one thread gives orders to other threads, and other threads will hold a threading Event object, these threads will wait for the "occurrence" of this event. If this event does not occur, these threads will block until the "occurrence" of the event. And this kind of scene is very common.

1.2 threading.Event properties and methods

threading.Event properties and methods
Serial numberProperties and methodsdescribe
1clear()Clear the signal flag inside the Event object, that is, set to False.
2is_set()/isSet()Judge the status of internal signal signs. When set() is used, the isSet() method returns True; When clear() is used, the isSet() method returns False.
3set()Set the signal flag inside the Event object to True.
4wait()This method will be executed and returned only when the internal signal is True. When the internal signal flag is False, wait() waits until it is True.

2. threading.Condition usage demonstration

#Design a red street lamp, threading Event is an indication of the direction of the car
#Within 10 seconds, there is a red light before 4 seconds, a green light from 4 seconds to 8 seconds, and a red light after 4 seconds

#Design an automobile class, traffic lights in the direction of the automobile, pass at the green light (set True) and wait at the red light (set False)
#Wait up to 10 seconds and then turn around; The timer is used to indicate the second when it comes to the intersection of red street lights

#Design a traffic light in the direction of human beings and cars. Wait at the green light (set True) and pass at the red light (set False)
#Wait up to 10 seconds and then turn around; The timer 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 is an indication of the direction of the car
# Within 10 seconds, there is a red light before 4 seconds, a green light from 4 seconds to 8 seconds, and a red light after 4 seconds

# Design an automobile class, traffic lights in the direction of the automobile, pass at the green light (set True) and wait at the red light (set False)
# Wait up to 10 seconds and then turn around; The timer 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 traffic light in the direction of human beings and cars. Wait at the green light (set True) and pass at the red light (set False)
# Wait up to 10 seconds and then turn around; The timer 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 the popularity of the blog is increased, I would like to thank you for writing a blog. If it is very popular, I would like to praise it!

'''

Keywords: Python Multithreading

Added by jhuaraya on Mon, 14 Feb 2022 11:39:25 +0200