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.
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:Copy codeCode: # 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:Copy codeThe Code is as follows: 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'