This class indicates the activities that run in a separate control thread. There are two ways to specify this activity, pass the callback object to the constructor, or rewrite it in the subclass.Run ()Method. Other methods (except constructors) should not be overwritten in the subclass. In other words, only_ Init __()AndRun ()Method is 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.Isalive ()Method to test whether the thread is active.
One thread can call other threadsJoin ()Method. This will block the call thread until it hasJoin ()Method thread call termination.
The thread has a name. The name can be passed to the constructor.Setname ()Method settings, useGetname ()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 usedSetdaemon ()Method settings, useIsdaemon ().
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 be used.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:
GroupIt should be none for future implementationThreadgroupClass extension.
TargetYesRun ()Method call callback object. The default value is none, which means no object is called.
NameIs the thread name. Default format: 'thread-NThe unique name of 'is created, whereNIs a relatively small decimal number.
ARGsIs the tuple of the target call parameter. The default value is ().
KwargsIs the keyword Dictionary of the parameter called by the target. The default value is {}.
If the subthread overwrites the constructor, it must ensure that the base class constructor is called.(Thread. _ init __())Before other work in the thread.
- Start ()
-
Start thread activity.
Each thread object can be called at most once. It arranges objectRun ()Called in a separate control thread.
-
Run ()
-
A method used to indicate thread activity.
-
You may override this method in the subclass. StandardRun ()Method callTargetThe callback object passed to the object constructor. If a parameter exists, a series of keyword parameters areARGsAndThe kwargs parameter works accordingly.
- Join ([Timeout])
-
Wait until the thread is terminated. This blocks the call thread until the thread Join ()The method is aborted by calling-normal exit or an unhandled exception is thrown-or an optional timeout occurs.
-
WhenTimeoutThe parameter is not set or is notNoneIt should be a floating point that specifies the operation timeout value in seconds. BecauseJoin ()Always returnNone, You must callIsalive ()To determine whether a timeout occurs.
-
WhenTimeoutThe parameter is not specified orNoneThe operation will be blocked until the thread is terminated.
-
The thread can beJoin () many times.
-
The thread cannot call its ownJoin (), because this will cause a deadlock.
-
Try to call before the thread startsJoin ()An error occurs.
- 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, the thread starts fromStart ()When the call startsRun ()When the method is aborted and returned, it is regarded as active. Module functionsEnumerate () returns the list of active threads.
- Isdaemon ()
-
Return the daemon mark of the thread.
- Setdaemon (Daemonic)
-
Set the daemon flag to a Boolean Value Daemonic. It must be in Start ()Called before the call.
-
The initial value is inherited to the creation thread.
-
When there is no active non-daemon thread, the entire Python program exits.