The Threading.thread class of the Python standard library (transferred from someone else's translation)

Source: Internet
Author: User
Tags terminates

This class represents an activity that runs in a separate control thread. There are two ways to specify this activity, pass a callback object to the constructor, or override the run () method in a subclass. Other methods (except constructors) should not be overridden in subclasses. In other words, only the __init__ () and Run () methods are overridden in subclasses.

Once the thread object is created, its activity needs to be started by calling the thread's start () method. This method calls the Run method in the control thread again.

Once the thread is activated, the thread is considered ' alive ' (activity). When its run () method terminates-gracefully exits or throws an unhandled exception, the active state stops. The isAlive () method tests whether the thread is active.

A thread can invoke the join () method of another thread. This will block the calling thread until the call to the thread that owns the join () method terminates.

Thread has a name. The name can be passed to the constructor, set by the SetName () method, and obtained using the getName () method.

The thread can be identified as ' daemon thread ' (daemon). This flag is characterized by the Python program exiting when all that is left is the daemon thread. Its initial value inherits from the creation thread. This flag is set with the Setdaemon () method and is obtained with Isdaemon () .

There is ' main thread ' (main thread), which corresponds to the initial control thread of the Python program. It is not a background thread.

It is possible that the ' dummy thread objects ' (dummy thread object) is created. These threads correspond to ' alien threads ' (external threads), which are launched outside of the Python threading model, as if they were launched directly from the C language code. Dumb threading objects have limited functionality, they are always considered active, daemon threads, and cannot use the join () method. They can never be removed, since it is not possible to monitor the abort of external threads.

Class Thread ( group=None, target=None, name=None, args=(), kwargs={} )

The constructor can be called with a keyword argument. These parameters are:

groupshould be None, reserved for future implementations of the Threadgroup class.

targetIs the callback object that is called by the Run () method. The default should be none, meaning no objects are called.

nameis the thread name. By default, the unique name of the form ' thread- N ' is created, which N is a smaller decimal number.

argsis the tuple of the target invocation parameter, which defaults to ().

kwargsIs the keyword dictionary for the parameter of the target invocation, which defaults to {}.

If the child thread overrides the constructor, it should ensure that the constructor of the base class is called (thread.__init__ ()), before other work is done in the thread.

Start ()
Initiates thread activity.

is called at most once per thread object. It arranges that the object's run () is called in a separate control thread.

Run ()

the method used to represent thread activity.

You may override this method in subclasses. The standard run () method call acts as a target callback object that is passed to the object constructor. If there is a parameter, a series of keyword arguments from args andkwargs参数相应地起作用。

Join ([ timeout ])
wait until the thread aborts. This blocks the calling thread until the thread's join () method is called abort-gracefully exits or throws an unhandled exception-or an optional timeout occurs.

When the parameter is not timeout set or not None , it should be a floating-point number indicating the operation timeout value in seconds. Because join () always returns None , you must call isAlive () to determine whether the timeout occurs.

When the timeout parameter is not specified or is None , the operation is blocked until the thread aborts.

Threads can be join () many times.

The thread cannot call its own join (), because this will cause a deadlock.

An error occurred attempting to call join () before the thread started.

GetName ()
returns the thread name.
SetName ( name )
sets the thread name.

The name is a string that is used for identity purposes only. It has no other effect. Multiple threads can take the same name. The initial name is set by the constructor function.

IsAlive ()
returns whether the thread is active.

In general, the thread is considered active from the start () call to the point where its run () method aborts the return. The module function enumerate () returns a list of active threads.

Isdaemon ()
returns the daemon thread flag for the thread.
Setdaemon ( daemonic )
sets the daemon flag to a Boolean value daemonic . It must be called before the start () call.

The initial value is inherited to the creation thread.

When there is no active non-daemon thread, the entire Python program exits.

The Threading.thread class of the Python standard library (transferred from someone else's translation)

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.