Python multi-thread threading and multiprocessing module instance parsing,

Source: Internet
Author: User

Python multi-thread threading and multiprocessing module instance parsing,

This article focuses on the Python multi-thread threading and multiprocessing modules. The details are as follows.

A thread is the entity of a process. It is composed of registers (such as program counters, stack pointers) and stacks that indicate the running status. It is a smaller unit than a process.
A thread is an execution stream in a program. An execution flow is formed by the CPU running program code and operating program data. Therefore, threads are considered to be CPU-based.

The thread does not contain the code and data in the process address space. The thread is the state of the computing process at a certain time point. Therefore, when the system generates a thread or switches between threads, the burden is much lower than that of the process.

A thread is a user-level entity. The thread structure is resident in the user space and can be directly accessed by common user-level functions.

A thread is not a program and must run in a program (process. Therefore, a thread can be defined as a single execution stream in a program.

Multithreading refers to a program that contains multiple execution streams. multithreading is an effective means to achieve concurrency. A process can generate multiple threads during its execution to form multiple execution streams. Each execution stream, that is, each thread also has its own production, existence, and extinction process.

Multi-threaded program design means that program tasks can be divided into several parallel subtasks.

Thread status chart:

Commonly used thread modules in Python

  • Thread (used in earlier versions), threading
  • Queue
  • Multiprocessing

Threading

The thread module is used in earlier versions of Python, and is replaced by threading in later versions. The threading module provides more convenient APIs to operate threads.

Threading. Thread

Thread is one of the most important classes in the threading module and can be used to create threads. There are two ways to create a new thread:

  • Method 1: directly create a threading. Thread class object. during initialization, the object can be called as a parameter.
  • Method 2: override the run method by inheriting the Thread class.

Thread class constructor:

__init__(group=None, target=None, name=None, args=(), kwargs=None, verbose=None)

Parameter description:

Group: thread group, which has not been implemented yet. The database reference prompts that it must be None.
Target: method to be executed;
Name: Specifies the thread name;
Args/kwargs: parameter of the method to be passed in.

Method of the Instance owned by the Thread class:

IsAlive (): returns whether the thread is running. Running indicates that the instance is started before it is terminated.

GetName (name)/setName (name): Get/set the thread name.

IsDaemon (bool)/setDaemon (bool): gets/sets whether the thread is a daemon. The initial value is inherited from the thread where the thread is created. When no non-daemon thread is still running, the program will terminate.

Start (): start the thread.

Join ([timeout]): blocks the thread in the current context until the thread that calls this method terminates or reaches the specified waiting time timeout (optional parameter ). That is, the current thread must wait until the thread that calls the join () method is executed completely, or the specified time has been reached.

Directly create a threading. Thread class Object

Instance:

From threading import Threadimport timedef run (a = None, B = None): print a, B time. sleep (1) t = Thread (target = run, args = ("this is a", "thread") # at this time, the Thread is in the new State print t. getName () # obtain the thread object name print t. isAlive () # checks whether the thread is still alive. T. start () # start thread t. join () # Wait until other threads finish running

Execution result:

Thread-1Falsethis is a thread

Note:

t = Thread(target = run, args = ("this is a", "thread"))

This statement only creates a thread and does not execute this thread. At this time, the thread is in the new State.

T. start () # start the thread

Start the thread. At this time, the thread is thrown to run, but in the preparation status.

The custom function run () allows us to customize the function name based on our own needs. The parameters of the run function are derived from the following args tuples.

Inherit the Thread class

Instance:

From threading import Threadimport timeclass MyThread (Thread): def _ init _ (self, a): super (MyThread, self ). _ init _ () # Call the constructor self of the parent class. a = a def run (self): print "sleep:", self. a time. sleep (self. a) t1 = MyThread (2) t2 = MyThread (4) t1.start () t2.start () t1.join () t2.join ()

Execution result:

Because two threads t1 and t2 are created for concurrent execution, the execution time of the concurrent thread is not fixed, and the execution time of the concurrent thread is not fixed. Therefore, the order of the printed results after execution is also unstable. Each execution may have different results.

Note:

The MyThread constructor of the new class that inherits the Thread class must call the constructor of the parent class to generate parameters in the constructor of the parent class and produce parameters required by the Thread. If other parameters are required in the new class, simply add them to the constructor.

In the new class, when rewriting the run method of the parent class, it does not contain parameters by default. If you need to provide it with parameters, you must specify them in the class constructor, in the thread execution process, the thread calls the run method by itself and does not need to be manually called. Therefore, parameters cannot be directly transmitted. You can only set parameters in the constructor, and then call it in the run method.

Examples of join () function usage:

# Encoding: UTF-8import threadingimport timedef context (tJoin): print 'in threadContext. 'tJoin. start () # Blocks tContext until threadJoin ends. TJoin. join () # tJoin continues after termination. Print 'out threadContext. 'def join (): print 'in threadJoin. 'Time. sleep (1) print 'out threadJoin. 'tjoin = threading. thread (target = join) tContext = threading. thread (target = context, args = (tJoin,) tContext. start ()

Execution result:

in threadContext.in threadJoin.out threadJoin.out threadContext.

Resolution:

After this tJoin = threading. Thread (target = join) Statement is executed in the main program, a Thread object tJoin is created, but the Thread is not started.

tContext = threading.Thread(target=context, args=(tJoin,))tContext.start()

After the preceding two statements are executed, create another thread object tContext and start the thread (print in threadContext .), at the same time, the tJoin thread object is passed as a parameter to the context function. In the context function, the tJoin thread is started, and the thread calls the join () function (tJoin. join (), then the tContext thread will wait until the execution of the tJoin thread is complete before it can continue after the tContext thread. Therefore, the join () function is executed first and the following two sentences are printed:

in threadJoin.out threadJoin.

After the execution of the tJoin thread ends, the tContext thread continues to be executed. Therefore, the output is out threadContext. Therefore, we can see the output results shown above, regardless of the number of executions, the results are in this order. However, if you use tJoin in the context () function. the join () clause is commented out, and then the program is executed. The order of output results is not fixed because the two threads are concurrently executed.

Multiprocessing. dummy

In Python, the thread multiprocessing module is the same as the process module. The usage is also basically the same. The only difference is that the import Pool such as from multiprocessing import Pool represents the process Pool;
The Pool imported from multiprocessing. dummy import Pool indicates the thread Pool. In this way, the concurrency in the thread can be realized.

Thread Pool instance:

Import timefrom multiprocessing. dummy import Pool as ThreadPool # Give the thread Pool an alias ThreadPooldef run (fn): time. sleep (2) print fnif _ name _ = '_ main _': testFL = [1, 2, 3, 4, 5] pool = ThreadPool (10) # create a thread pool with 10 capacities for concurrent execution. map (run, testFL) pool. close () pool. join ()

Execution result:

Here, the pool. map () function is used in the same way as the map function in the process pool, and is also used in the same way as the built-in map function.

Summary

The above is all the content about the instance parsing of the Python multi-thread threading and multiprocessing modules. I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.