python notes 12-python multithreading event (Event)

foreword

Friends a, b, c eat hot pot around, when the dishes are ready, the host of the treat said: let's eat! , so the little friends move the chopsticks together, how to realize this scene

Event

Event (event): The mechanism of event processing: a built-in flag Flag is defined globally. If the Flag value is False, then the program will block when the event.wait method is executed. If the Flag value is True, then the event.wait method will be blocked. no longer block.

Event is actually a simplified version of Condition. Event has no lock and cannot make the thread enter a synchronous blocking state.

Event()

  • set(): Set the flag to True and notify all threads that are in the waiting blocked state to resume running.

  • clear(): Set the flag to False.

  • wait(timeout): If the flag is True, it will return immediately, otherwise block the thread to wait blocking state, waiting for other threads to call set().

  • isSet(): Get the built-in flag status, return True or False.

Event case 1

Scenario: small partners a and b are ready, when they receive the notification event.set(), the a and b threads will be executed

# coding:utf-8

import threading
import time

event = threading.Event()


def chihuoguo(name): #Wait
     for an event, enter the waiting blocking state 
    print  ' %s has started ' % threading.currentThread().getName()
     print  ' My friend %s has entered the dining state! ' % name
    time.sleep(1)
    event.wait()
    #Enter the running state after receiving the event 
    print  ' %s received the notification. ' % threading.currentThread().getName()
     print  ' My friend %s starts eating! ' % name

#Set thread group 
threads = []

#Create a new thread 
thread1 = threading.Thread(target=chihuoguo, args=( " a " , ))
thread2 = threading.Thread(target=chihuoguo, args=("b", ))

#Add to thread group 
threads.append(thread1)
threads.append(thread2)

#Open thread 
for thread in threads:
    thread.start()

time.sleep( 0.1 )
 #Send event notification 
print  ' The main thread notifies the friends to start eating! ' 
event.set()

 

operation result:

Thread-1 已经启动
小伙伴 a 已经进入就餐状态!
Thread-2 已经启动
小伙伴 b 已经进入就餐状态!
主线程通知小伙伴开吃咯!
Thread-1 收到通知了. 小伙伴 a 开始吃咯! Thread-2 收到通知了. 小伙伴 b 开始吃咯!

Event case 2

Scene: When friends a, b, and c are assembled, the person who treats the guests speaks: Let's eat!

# coding:utf-8

import threading
import time

event = threading.Event()


def chiHuoGuo(name): #Wait
     for an event, enter the waiting blocking state 
    print  ' %s has started ' % threading.currentThread().getName()
     print  ' My friend %s has entered the dining state! ' % name
    time.sleep(1)
    event.wait()
    #Enter the running state after receiving the event 
    print  ' %s received the notification. ' % threading.currentThread().getName()
     print  ' %s buddy %s starts eating! ' % (time.time(), name)


class myThread (threading.Thread): #Inherit    the parent class threading.Thread 
    def  __init__ (self, name):
         ''' Rewrite the initialization content of threading.Thread ''' 
        threading.Thread .__ init__ (self)

        self.people = name

    def run(self): #Write    the code to be executed into the run function. After the thread is created, it will run the run function directly. 
        ''' Rewrite the run method '''

        chiHuoGuo(self.people) #Execute      task print 
        ( " qq communication group: 226296743 " )
         print ( " End thread: %s " % threading.currentThread().getName())

#Set the thread group 
threads = []
 #Create a new thread 
thread1 = myThread( " a " )
thread2 = myThread("b")
thread3 = myThread("c")

#Add to thread group 
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)

#Open thread 
for thread in threads:
    thread.start()

time.sleep( 0.1 )
 #Send event notification 
print  ' The collection is complete, the staff is here, let's eat! ' 
event.set()

 

operation result:

Thread-1 已经启动
小伙伴 a 已经进入就餐状态!
Thread-2 已经启动
小伙伴 b 已经进入就餐状态!
Thread-3 已经启动 小伙伴 c 已经进入就餐状态! 集合完毕,人员到齐了,开吃咯! Thread-1 收到通知了. 1516780957.47 小伙伴 a 开始吃咯! 结束线程: Thread-1 Thread-3 收到通知了. 1516780957.47 小伙伴 c 开始吃咯!Thread-2 收到通知了. 1516780957.47 小伙伴 b 开始吃咯!结束线程: Thread-3 结束线程: Thread-2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337525&siteId=291194637