Python implements the blocking mode similar to the AutoResetEvent class in C #.

Source: Internet
Author: User

People who have worked on C # multithreading understand AutoResetEvent and ManualResetEvent, including the WaitOne () method, Set () and Reset () method () many methods are used in thread synchronization.

AutoResetEvent: When a thread executes the WaitOne () method, the thread is in blocking mode. When the Set () method is called, The blocked thread continues to run down, its status is automatically set to blocking mode immediately.

ManualResetEvent: When a thread executes the WaitOne () method, the thread is in blocking mode. When the Set () method is called, The blocked thread continues to run down, its status is not automatically set to blocking mode. You must call its Reset () method to set its status to blocking mode. Www.2cto.com

In python, the threading module also has a similar thread blocking mode class threading. event (). This class is similar to the ManualResetEvent class of C #. It cannot automatically change to blocking mode after the set () method is called. Sometimes we need this automatic blocking mode in the development project, and I have encountered this requirement in the project, so I tried to write a similar class myself, the Code has been posted and shared. There are not many codes. We hope that the prawns or elders can point out the incorrect or unreasonable code. Thank you!

The Code is as follows:

[Python]
# Encoding: UTF-8
 
Import threading
 
Class AutoEvent:
Def _ init _ (self ):
Self. event = None
Self. is_wait = False # whether it is in the blocking status
 
Def Wait (self, timeout = None ):
If not self. is_wait:
Self. is_wait = True
Self. event = threading. Event () # instantiate threading. Event () object
Self. event. wait (timeout = timeout) # Call the threading. Event (). wait () method to make the thread in a blocking state
Del self. event # release an object
 
Def Set (self ):
If not self. is_wait: raise 'must be invoke Wait () method before Set ()'
Self. is_wait = False
Self. event. set () # Call the threading. Event (). set () method to keep the thread in

The following is a Demo for testing:
[Python]
Import wx
 
AutoEvent = AutoEvent () # instantiate the MamualEvent object
 
Class TestDialog (wx. Dialog ):
Def _ init _ (self, parent ):
Wx. Dialog. _ init _ (self, parent,-1)
 
B = wx. Button (self,-1, "print", (50,140 ))
Self. Bind (wx. EVT_BUTTON, self. OnPrint, B)
 
Self. thread = threading. Thread (target = self. work)
Self. thread. start ()
 
Def work (self ):
While True:
Print 3
AutoEvent. Wait () # Blocking
 
Def OnPrint (self, evt ):
AutoEvent. Set () # continue
 
If _ name __= = "_ main __":
App = wx. App ()
F = TestDialog (parent = None)
F. Show ()
App. MainLoop ()

When the program is executed, a '3' will be printed first, and a '3' will be printed without clicking the print button'
The test is successful.


Note: If you set autoEvent In the Demo code. wait () and autoEvent. set () to threading. the wait () and set () Methods of the Event () class will return the following results: when the program is executed, a '3' will be printed, and then click the print button, print '3' repeatedly'

 

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.