A _c# tutorial on Python implementation of AutoResetEvent class blocking mode method

Source: Internet
Author: User
Tags in python
People who have worked on C # Multithreading understand the two classes of AutoResetEvent and ManualResetEvent, where the WaitOne () method and set () and Reset () methods are used more in thread synchronization.
AutoResetEvent: When a thread executes to the WaitOne () method, the thread is in blocking mode, and when the set () method is invoked, the blocked thread continues to execute downward and its state is automatically set to blocking mode.
ManualResetEvent: When a thread executes to the WaitOne () method, the thread is in blocking mode, and when the set () method is invoked, the blocked thread continues down, its state is not automatically set to blocking mode, and its reset () must be called. method to set its state to blocking mode.

There is also a class threading in Python with a similar thread blocking pattern under the threading module. Event (), which is similar to the ManualResetEvent class in C #, and cannot automatically become blocked after invoking the set () method. There are times when we need this automatic blocking pattern in our development projects, I also encountered this demand in the project, I tried to write a similar class, bar code posted out to share, the code is not much, there is no or unreasonable place to hope prawns or predecessors can point out, thank you!
The code is as follows:
Copy Code code as follows:

# Encoding:utf-8
Import threading
Class Autoevent:
def __init__ (self):
Self.event = None
self.is_wait = False # is in a blocked state
def wait (Self,timeout=none):
If not self.is_wait:
self.is_wait = True
Self.event = Threading. Event () # instantiates threading. Event () object
Self.event.wait (timeout=timeout) # calls threading. Event (). Wait () method, leaving the thread in a blocked state
Del self.event # Release Object
def Set (self):
If not self.is_wait:raise ' must is Invoke wait () method before Set () '
self.is_wait = False
Self.event.set () # calls threading. Event (). Set () method, leaving the thread on

Here is the demo, used to test:
Copy Code code as follows:

Import WX
Autoevent = Autoevent () # Instantiate 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 executes, first will print out a ' 3 ', after not clicking the Print button, will print a ' 3 '
The test was successful.
Attention: If you change the autoevent.wait () and Autoevent.set () in the demo code to threading. The wait () and set () methods of the Event () class will have the following result: When the program executes, it prints a ' 3 ', and after clicking the Print button, it loops through the ' 3 '

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.