[Python journey] Article 6 (III): Python multithreading and usage

Source: Internet
Author: User

[Python journey] Article 6 (III): Python multithreading and usage
To learn the Multi-threading of Python, at least the basic concepts of processes and threads should be involved. You can refer to the article I have posted: a simple explanation of processes and threads. When I used Python Socket to write a simple SSH program, I used multithreading. However, I was not very familiar with the concept of multithreading, after reading a simple explanation of processes and threads and learning Python multithreading, I have a rough understanding. The content is as follows:

1. multithreading in Python 2. How to Use Python multithreading 13. How to Use Python multithreading 2

 

1. multiple Threads in Python execute a program, that is, a process is started in the operating system. At a certain time point, a CPU core can only execute one process task, the multi-process/multi-task mentioned by the current computer is actually achieved by accelerating the execution speed of the CPU, because one CPU can execute hundreds of millions of computations per second and can perform many switches on the process, therefore, in a human-perceptible time, it seems that the computer is indeed executing multiple programs at the same time, that is, processing multiple processes at the same time. A process can contain multiple threads. These threads run to implement a major function of the process. Multiple Threads can perform serial work, it can also work concurrently. Obviously, the latter can save more time. In Python, multi-thread concurrent execution is supported, but only single-core threads can be used in Python. That is to say, multiple threads of a certain process in Python can only run on one CPU core, but cannot be allocated to run in multiple CPU cores. This is because of thread security, while GIL in Python ensures thread security. For details about GIL in Python, refer to the following article: Analysis of GIL and thread security in Python. The following are some of my classroom notes during the learning process. Because I have not actually learned some theories, I may be wrong, but it is easy for me to understand: that is, GLI controls global locks Based on the CPU core. Therefore, GLI cannot be used across different CPUs (cores) to ensure that in the same process, shared data of a thread can only be modified (used) by another thread at a certain time, but cannot be modified (used) by multiple threads at the same time. If GLI is removed, you need to lock the thread yourself, so that the performance is worse than the original. Of course, isn't it possible to make full use of multiple CPU cores or multiple CPUs? You can create multiple processes. Different processes run on different CPUs (cores) and can also implement concurrency. However, this will result in a waste of memory space, consider running 10 QQ programs at the same time. If one QQ account occupies MB of memory, the 10 QQ accounts for 5 GB of memory. However, if it is multi-threaded, 10 QQ may still share the MB memory space. Another drawback is that direct access to data between multiple processes may be troublesome, but it can also be implemented. For example, chrome is implemented by multiple processes. At present, it should be clear that in Python, multiple threads of a process cannot be distributed across different CPU cores. 2. How to use multiple threads in Python 1. The following program code and comments are provided:
Import threading # import timedef run (num): print 'Hi, I am thread % s .. lalala '% num time. sleep (1) for I in range (20): t = threading. thread (target = run, args = (I ,))

 

# How to use multiple threads. target is the function that needs to execute multiple threads, and args is the parameter in the function. Note that the parameter here is written as (I,), that is, if only one parameter is allowed, add "," t. start () # The result of starting to execute a multi-threaded program is as follows:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python thread4.py Hi, I am thread 0..lalalaHi, I am thread 1..lalalaHi, I am thread 2..lalalaHi, I am thread 3..lalalaHi, I am thread 4..lalalaHi, I am thread 5..lalalaHi, I am thread 6..lalalaHi, I am thread 7..lalalaHi, I am thread 8..lalalaHi, I am thread 9..lalalaHi, I am thread 10..lalalaHi, I am thread 11..lalalaHi, I am thread 12..lalalaHi, I am thread 13..lalalaHi, I am thread 14..lalalaHi, I am thread 15..lalalaHi, I am thread 16..lalalaHi, I am thread 17..lalalaHi, I am thread 18..lalalaHi, I am thread 19..lalala

 

The execution result cannot be seen directly. Here we will talk about the execution process of this program: 0 to 19 print the input at the same time. After printing 19, the program stops running after 1 second of sleep. The above program has 20 threads to execute, and each thread is: print string + sleep (1 ). The actual result we see is 0 to 19 printing at the same time, and then sleep for 1 second, but it should be noted that not only 20 threads run a sleep (1 ), instead, a sleep (1) is executed in each thread, that is, the program actually runs 20 sleep (1) times ), the actual result is that the program is paused for only one second, because the 20 sleep (1) runs concurrently. The above program can understand this: 20 threads are equivalent to 20 horses, 20 horses start at the same time (print strings), and then stop for 1 second (sleep (1) at the same time )), the end point is reached at the same time (20 threads have finished running, that is, program execution has ended ). To better understand the above program, you can change the above Code to the following:
Import threadingimport timedef run (num): print 'Hi, I am thread % s .. lalala '% num time. sleep (1) for I in range (20): t = threading. thread (target = run, args = (I,) t. start () t. join () # Wait until the previous thread finishes executing and then execute the next thread

 

The execution result is as follows:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python thread4.py Hi, I am thread 0..lalalaHi, I am thread 1..lalalaHi, I am thread 2..lalalaHi, I am thread 3..lalalaHi, I am thread 4..lalalaHi, I am thread 5..lalalaHi, I am thread 6..lalalaHi, I am thread 7..lalalaHi, I am thread 8..lalalaHi, I am thread 9..lalalaHi, I am thread 10..lalalaHi, I am thread 11..lalalaHi, I am thread 12..lalalaHi, I am thread 13..lalalaHi, I am thread 14..lalalaHi, I am thread 15..lalalaHi, I am thread 16..lalalaHi, I am thread 17..lalalaHi, I am thread 18..lalalaHi, I am thread 19..lalala

 

The execution result looks the same as the previous one, but the execution process is like this: Every time a string is printed, It is paused for another second. Through this program, we can better understand the concurrent execution of multiple threads in Python. Of course, because this is a dynamic process, we will have a better understanding after executing the program again. 3. Python multithreading method 2 the program code is as follows:
import threading,timeclass MyThread(threading.Thread):    def __init__(self, num):        threading.Thread.__init__(self)        self.num = num    def run(self): #this name must be 'run'        print 'I am thread %s' % self.num        time.sleep(2)for i in range(20):    t = MyThread(i)    t.start()

 

The execution result of the program is the same as that of method 1, which is not provided here, but the idea and method of object-oriented programming are used to design the program code.

Related Article

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.