A detailed description of the use of Python multi-thread event events

Source: Internet
Author: User
This article mainly introduces the use of Python multi-thread event events, and now share to everyone, but also to make a reference. Come and see it together.

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 ()

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

    2. Clear (): Sets the flag to false.

    3. 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 ().

    4. 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-8import Threadingimport timeevent = Threading. Event () def Chihuoguo (name):  # waits for event, enters wait blocking state  print '%s has started '% threading.currentthread (). GetName ()  print ' Small partner%s has entered the dining state! '%name  time.sleep (1)  event.wait ()  # received an event and went into  the running state print '%s was notified. '% Threading.currentthread (). GetName ()  print ' small partner%s start eating! '%name# set Thread Group threads = []# creates a new thread Thread1 = threading. Thread (Target=chihuoguo, args= ("a",)) Thread2 = Threading. Thread (Target=chihuoguo, args= ("B",)) # added 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 ' main thread notification small partner Open eat! ' Event.set ()

Operation Result:

Thread-1 has been activated.
Small partner A has entered the dining state!
Thread-2 has been activated.
Small Partner B has entered the dining state!
The main thread to inform the small partners to eat!
Thread-1 received a notice.
Little buddy a starts eating!
Thread-2 received a notice.
Little buddy B starts eating!

Event Case 2

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

# coding:utf-8import Threadingimport timeevent = Threading. Event () def Chihuoguo (name): # waits for event, enters wait blocking state print '%s has started '% threading.currentthread (). GetName () print ' small partner%s ' has entered the repast State!  '%name time.sleep (1) event.wait () # received an event and went into run status print '%s was notified. '% Threading.currentthread (). GetName () print '%s small partner %s Start eating! '% (Time.time (), Name) class MyThread (threading. Thread): # inherits the parent class threading. Thread def __init__ (self, Name): "' rewrite threading. Thread initializes the content "' Threading. Thread.__init__ (self) self.people = name def run (self): # Write the code you want to execute into the run function, and the thread will run directly after it has been created to "rewrite the Run Method" Chihu Oguo (self.people) # Execute task print ("QQ Exchange Group: 226296743") print ("End thread:%s"% Threading.currentthread (). GetName ()) # Set Thread group th Reads = []# creates a new thread Thread1 = MyThread ("a") Thread2 = MyThread ("b") Thread3 = MyThread ("C") # Added 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 ' collection finished, personnel to the QI, open eat! ' Event.set ()

Operation Result:

Thread-1 has been activated.
Small partner A has entered the dining state!
Thread-2 has been activated.
Small Partner B has entered the dining state!
Thread-3 has been activated.
Small partner C has entered the dining state!
The collection is complete, the personnel to the QI, open eat!
Thread-1 received a notice.
1516780957.47 little buddy a starts eating!
QQ Exchange Group: 226296743
End Thread: Thread-1
Thread-3 received a notice.
1516780957.47 Small partner C start eating! Thread-2 received a notice.
QQ Exchange Group: 226296743

1516780957.47 Small partner B start eating! End Thread: Thread-3

QQ Exchange Group: 226296743
End Thread: Thread-2

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.