Pause, restore, exit, and instance of python threads, and details of python
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:
This completes the pause, resume, and stop functions. however, there is a drawback: Neither pause nor stop is instantaneous. It is valid only when the internal running of the run function reaches the flag. that is to say, the operation will be delayed once.
But sometimes this is not necessarily a bad thing. if the run function involves file operations or database operations, and then exits after a complete operation, it can execute the code for the remaining resource release operations (such as various close operations ). there will be no embarrassing situations such as program file operators exceeding the upper limit and database connections not released.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!