Pause, resume, and exit the python thread

Source: Internet
Author: User
We all know that the threading module in python can implement multithreading, but the module does not provide methods to pause, recover, and stop threads. Once the thread object calls the start method, you can only wait until the corresponding method function is completed. that is to say, once started, the thread is out of control. however, we can implement this by ourselves. average Pause, resume, and exit the python thread.

We all know that the threading module in python can implement multithreading, but the module does not provide a method to pause, recover, or stop a thread. Once the thread object calls the start method, you can only wait until the corresponding method function is completed. that is to say, once started, the thread is out of control. however, we can implement this by ourselves. the general method is to cyclically determine a flag. Once the flag reaches the predefined value, it exits the loop. in this way, the thread can be exited. however, it is a bit difficult to pause and recover the thread. I have never cleared any good methods until I see the description of the wait method of the Event object in threading.

Wait ([timeout]) Block until the internal flag is true. if the internal flag is true on entry, return immediately. otherwise, block until another thread callset () to set the flag to true, or until the optional timeout occurs. blocking until the internal flag is True. if the inner flag is True at entry time, return immediately. otherwise, it is blocked until other threads call the set () method to set the standard BIT to True or reach the optional timeout time. when the timeout argument is present and not None, it shocould be a floating Point number specifying a timeout for the operation in seconds (or fractions thereof ). this method returns the internal flag on exit, so it will always return True when T if a timeout is given and the operation times out. if the timeout parameter is not set to None, it should be a floating point number that specifies the operation timeout (or score) in seconds ). This method returns an internal flag when exiting. Therefore, it always returns True unless a timeout value is specified and the operation times out. Changed in version 2.7: previusly, before the method always returned None. 2.7, this method always returns None.



With the wait blocking mechanism, you can pause and resume the service. then, you can exit the service by using the loop identification space. The following is a sample code:

#! /Usr/bin/env python # coding: utf-8import threadingimport timeclass Job (threading. thread): def _ init _ (self, * args, ** kwargs): super (Job, self ). _ init _ (* args, ** kwargs) self. _ flag = threading. event () # ID used to suspend a thread self. _ flag. set () # set to True self. _ running = threading. event () # ID used to stop a thread self. _ running. set () # set running to True def run (self): while self. _ running. isSet (): self. _ flag. wait () # returns immediately when it is True. if it is False, blocking is returned until the internal flag is True. time () time. sleep (1) def pause (self): self. _ flag. clear () # set to False to block the thread def resume (self): self. _ flag. set () # set to True to stop the thread from blocking def stop (self): self. _ flag. set () # Restore the thread from the paused state. self. _ running. clear () # set to False



The following is the test code:

a = Job()a.start()time.sleep(3)a.pause()time.sleep(3)a.resume()time.sleep(3)a.pause()time.sleep(2)a.stop()



Test results:


-->

The above describes the pause, recovery, and exit details of the python thread and the details of the instance. For more information, see other related articles in the first PHP community!

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.