Python notes 12-python Multi-thread events (event)

Source: Internet
Author: User

Objective

Small partners a,b,c around to eat hot pot, when the food is QI, the host said: Open eat! , so the small partners to move chopsticks together, this scene how to achieve

Event (events)

Event: A mechanism for handling events: A built-in flag flag is defined globally, and if the flag value is False, it blocks when the program executes the Event.wait method, and if the flag value is true, then the Event.wait method is no longer blocked.

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

Event ()

    • Set (): Sets the flag to true and notifies all threads waiting to be blocked to resume running state.

    • Clear (): Sets the flag to false.

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

    • IsSet (): Gets the built-in flag State, which returns TRUE or false.

Event Case 1

Scenario: Small Partners A and B are ready to execute a and B threads when they receive notification event.set ()

#Coding:utf-8ImportThreadingImporttimeevent=Threading. Event ()defChihuoguo (name):#Wait for event, enter wait blocking state    Print '%s is already started'%Threading.currentthread (). GetName ()Print 'Small partner%s has entered the dining state! '%name Time.sleep (1) event.wait ()#Enter the running state after receiving an event    Print '%s received a notification.'%Threading.currentthread (). GetName ()Print 'Small partner%s start eating! '%name#set up thread groupsThreads = []#Create a new threadThread1 = Threading. Thread (Target=chihuoguo, args= ("a", )) Thread2= Threading. Thread (Target=chihuoguo, args= ("b", ))#add to Thread groupthreads.append (thread1) threads.append (thread2)#Open Thread forThreadinchThreads:thread.start () time.sleep (0.1)#Send event NotificationsPrint 'The main thread to inform the small partners to eat! 'Event.set ()

Operation Result:

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

Scene: When the small partners a,b,c assembled, the guests said: Open eat!

#Coding:utf-8ImportThreadingImporttimeevent=Threading. Event ()defChihuoguo (name):#Wait for event, enter wait blocking state    Print '%s is already started'%Threading.currentthread (). GetName ()Print 'Small partner%s has entered the dining state! '%name Time.sleep (1) event.wait ()#Enter the running state after receiving an event    Print '%s received a notification.'%Threading.currentthread (). GetName ()Print '%s Small partner%s start eating! '%(Time.time (), name)classMyThread (Threading. Thread):#inherits the parent class threading. Thread    def __init__(self, name):" "rewrite threading. Thread initialization Content" "Threading. Thread.__init__(self) self.people=namedefRun (self):#write the code you want to execute into the run function. The thread runs the run function directly after it is created        " "overriding the Run method" "Chihuoguo (self.people)#Perform Tasks        Print("QQ Exchange Group: 226296743")        Print("End Thread:%s"%Threading.currentthread (). GetName ())#set up thread groupsThreads = []#Create a new threadThread1 = MyThread ("a") Thread2= MyThread ("b") Thread3= MyThread ("C")#add to Thread groupthreads.append (thread1) threads.append (thread2) threads.append (THREAD3)#Open Thread forThreadinchThreads:thread.start () time.sleep (0.1)#Send event NotificationsPrint 'The collection is complete, the personnel to the QI, open eat! 'Event.set ()

Operation Result:

Thread-1 has started a small partnerA has entered the dining state!thread-2 has started small partners b has entered the dining state! thread-3 has started small partners c has entered the dining state! The collection is complete, the personnel to the QI, open eat! thread-1 received the notification. 1516780957.47 small partner a began to eat!  End thread: thread-1 Thread-3 received a notice. 1516780957.47 small partner c start eating! thread-2 received the notice. 1516780957.47 small partner b began to eat! End Thread: thread-3 End thread: thread-2            

Python note 12-python multi-thread events (event)

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.