As an inheritance of the ABC language, the Python programming language helps developers solve some difficult problems. In addition, it is easy to use and greatly improves the development efficiency. We will introduce the usage of the Python Thread class in detail today.
- Analysis of basic Python naming conventions
- Python Incremental Backup implementation tips
- Python data conversion code in-depth analysis
- In-depth exploration of the magic of the Python Sequence
- Python for in code Operation Method
The Python Thread class indicates the activity that runs in a separate control Thread. There are two ways to specify this activity, pass the callback object to the constructor, or rewrite the run () method in the subclass. Other methods except constructors should not be overwritten in the subclass. In other words, only the _ init _ () and run () methods in the subclass are overwritten.
Once a thread object is created, its activity needs to be started by calling the start () method of the thread. This method then calls the run method in the control thread.
Once the thread is activated, the thread is considered as 'alive' (active ). When its run () method is terminated-normal exit or an unhandled exception is thrown, the activity stops. The isAlive () method tests whether the thread is active.
A thread can call the join () method of another thread. This will block the call thread until the call of the thread that owns the join () method ends.
The thread has a name. The name can be passed to the constructor. It is set using the setName () method and obtained using the getName () method.
The thread can be identified as a 'daemon thread' (daemon thread). This indicates that the Python program exits when all the remaining threads are daemon threads. Its initial value is inherited from the creation thread. This flag is set using the setDaemon () method and obtained using isDaemon.
The 'main thread' (main thread) exists, which corresponds to the initial control thread of the Python program. It is not a background thread.
Some 'dummy thread objects' may be created. These threads correspond to 'alien Threads' (external threads) and are started outside of the Python thread model, such as directly starting from the C language code. Dumb thread objects have only limited functions. They are always considered active, daemon threads, and cannot use the join () method. They can never be deleted, since it cannot monitor the suspension of external threads.
Class Thread (group = None, target = None, name = None, args = (), kwargs = {})
The constructor can be called with a keyword parameter. These parameters are:
The group value should be None, which will be retained for future extension of the Python Thread class.
Target is the callback object called by the run () method. The default value is None, meaning no object is called.
Name indicates the thread name. By default, the unique name in the form of 'thread-n' is created, where N is a relatively small decimal number.
Args is the tuple of the target call parameter. The default value is ().
Kwargs is the keyword dictionary of the parameter called by the target. The default value is {}.
If the subthread overwrites the constructor, it should ensure that the constructor of the base class (Thread. _ init _ () is called before other work is performed in the Thread.
Start ()
Start thread activity.
Each thread object can be called at most once. It schedules the object's run () to be called in a separate control thread.
Run ()
A method used to indicate thread activity.
You may rewrite this method in the subclass of the Python Thread class. The standard run () method call serves as the callback object that the target passes to the object constructor. If a parameter exists, a series of keyword parameters take effect from The args and kwargs parameters accordingly.
Join ([timeout])
Wait until the thread is terminated. This blocks the call thread until the join () method of the thread is called to stop-exit normally or throw an unhandled exception-or an optional timeout occurs.
When the timeout parameter is not set or is not None, it should be a floating point that specifies the operation timeout value in seconds. Because join () always returns None, you must call isAlive () to determine whether a timeout occurs.
When the timeout parameter is not specified or None, the operation is blocked until the thread is terminated.
The thread can be joined () many times.
The thread cannot call its own join (), because this will cause a deadlock.
An error occurs when you try to call join () before the thread starts.
GetName ()
The name of the returned thread.
SetName (name)
Set the thread name.
This name is a string used only to identify the target. It has no other effect. Multiple Threads can have the same name. The initial name is set through the constructor.
IsAlive ()
Returns whether the thread is active.
Generally, when the thread starts from the start () call to its run () method, it is considered to be active. The module function enumerate () returns the list of active threads.
IsDaemon ()
Return the daemon mark of the thread.
SetDaemon (daemonic)
Set the daemonic flag to a Boolean value. It must be called before start () is called.
The initial value is inherited to the creation thread.
When there is no active non-daemon thread, the entire Python program exits.
The above is our introduction to the Python Thread class.