Python create a thread

Python provides _thread and threading two modules to support multi-threading, which provides low-level _thread, the original thread support, and a simple lock, as its name implies, is generally not recommended thread programming module; the threading module provides a feature-rich multi-threading support.

Python main thread is created in two ways:

Use threading module of the Thread class constructor creates a thread. 

Thread class inheritance threading module to create a thread class. 

Call the Thread class constructor creates a thread 

calls the Thread class constructor creates a thread is simple, direct call follows the constructor threading.Thread class to create a thread: 

__init __ (Self, Group = None, target = None, name = None, args = (), kwargs = None, * , daemon = None)

The above relates to the constructor several parameters:

group: Specifies the thread group that thread belongs. This parameter is currently not yet been realized, so it can only be set to None.

target: target method of this thread to be scheduled.

args: Specifies a tuple, in the form of positional parameters specified for the target function of parameters passed. The first element of the tuple argument passed to a function of the first target, the second element of the tuple argument passed to a second target function ...... and so on.

kwargs: Specify a dictionary, in the form of keyword parameters specified for the target function parameter passing.

daemon: Specifies the thread is constructed for future generations thread.

To create a well through the Thread class constructor start multiple threads are as follows:

Call the constructor of the Thread class to create a thread object. When you create a thread object, target parameters specified function as a thread of execution.

Call the thread object's start () method to start the thread.

The following program demonstrates to create a thread object through the Thread class constructor:

Import threading 

# define a common action function, which is prepared as the thread of execution 
DEF action (max): 
    for I in Range (max): 
        # call threading module current_thread () function to get the current thread 
        # thread object getName () method get the current thread's name 
        Print (threading.current_thread () getName () + "" + str (i).) 
# the following is the main program (that is, the main thread of execution thereof) 
for i in the Range (100): 
    # call threading module current_thread () function to get the current thread 
    Print (threading.current_thread () getName () + "" + str (i).) 
    IF i == 20: 
        # create and start the first thread 
        t1 = threading.Thread (target = Action, args = (100,)) 
        t1.start () 
        # create and start a second thread 
        T2 = threading.Thread (target = Action, args = (100,)) 
        t2.start ()
print ( 'the main thread execution is completed!')

The above program contains a main program loop, when the loop variable i is equal to 20 to create and launch two new thread:

Creating a Thread object, target of the thread is action, action which means it will function as a thread of execution. Next, the program calls start () method to start a thread t1.

Once again create a thread that create and start a thread exactly the same way as the first.

Run the above procedures, you will see the interface shown in FIG.

2-1Z3041H32C17.jpg

While the above program only explicitly create and start two threads, but in fact the program has three threads, that is, two sub-thread and the main thread of the program explicitly created. Mentioned earlier, when the program starts running after the Python, the program creates at least one main thread, the main thread of execution thread is in the main program (not on any function in the code).

As can be seen from the figure, when the program CCP comprises three threads, the thread does not perform these three order, which perform in a concurrent manner: Thread-1 is performed for a time, then Thread-2 may be executed by a CPU or a period obtained MainThread time, followed by another for the other threads of execution, which is typical of concurrently executing threads, CPU fast rotation of mode switching between multiple threads, giving the user the illusion that multiple threads at the same time seems to perform.

Not difficult to see through the introduction of multi-threaded meaning of the above, if you do not use multiple threads, the main program twice a direct call action () function, then the ranking procedure must first call the action () function is executed, will perform a second call the action () function; must wait for the second call of the action () function is executed, the main program will continue downward.

This article is reproduced in https://www.py.cn/jishu/jichu/10902.html

Guess you like

Origin www.cnblogs.com/jsdd/p/11609220.html