"Python Tour" seventh (i): Another talk about Python multithreading

Source: Internet
Author: User

The main step is to further deepen the understanding of the multi-threaded correlation function join () in Python to solve the multi-threaded execution process. The following example is used to further illustrate this.


1. Sequence of execution of multi-threaded and main program code

Give the following program code:

#!/usr/bin/env pythonimport threadingimport timedef sayhi (n): Time.sleep (1) print ' Hi This is thread%s '%nthread_list = [] #用来存放多线程执行返回的函数入口, so the function entry is stored here for I in range: Thread_list.append (threading. Thread (Target=sayhi, args= (i,)))) for I in Thread_list: #执行列表中存放的函数入口, the program Code of SAYHI () is executed only after execution. Otherwise, it just stores the function entry in this list. I.start () for I in Thread_list: #检测对应的线程是否已经执行完毕 (that is, whether the function entry has been executed to execute the associated program code) I.join () #join () can be super Time, such as I.join (3), indicates that the current I corresponding to the function of the entry execution of the thread, if within 3 seconds has not completed the time-out, the thread can be executed normally without any relationship, but when the threads are abnormal and unable to perform properly, because I.join () It has not been detected that the thread has been executed, so it will always be in a wait state, which will cause the code of the program can not continue to execute (the program is still stuck in the I.join () here to wait for this one of its corresponding thread execution), set the time-out to avoid this problem, there will be relevant instructions print ' \033[32;1mthis is the last line\033[0m '

The results of the program execution are as follows:

[email protected]:/mnt/hgfs/python/day7$ python  Day6thread1.py hi this is thread 0hi this is thread 1hi this  is thread 2hi this is thread 3hi this is thread 4hi  this is thread 5hi this is thread 6hi this is thread  7Hi this is thread 8Hi this is thread 9 Hi this  Is thread 11 hi this is thread 14 hi this is thread  17 hi this is thread 12hi this is thread 15hi this  is thread 16hi this is thread 10hi this is thread 13hi  this is thread 18hi this is thread 19this is the last  line 

Modify the program code to read as follows:

#!/usr/bin/env pythonimport threadingimport timedef sayhi (n): Time.sleep (1) print ' Hi This is thread%s '%nthread_list = []f Or I in range: Thread_list.append (threading. Thread (Target=sayhi, args= (i,)))) for I in Thread_list:i.start () #for i in Thread_list: #i. Join () print ' \033[32;1mthis is The last line\033[0m '

The results of the program execution are as follows:

[email protected]:/mnt/hgfs/python/day7$ python day6thread1.py this is the  Last linehi this is thread 0 hi this is thread 2 hi  this is thread 6 hi this is thread 9 hi this is  thread 10 Hi this is thread 15 Hi this is thread  16 hi this is thread 3hi this is thread 8hi this is  thread 11 hi this is thread 14hi this is thread 17hi  this is thread 5hi this is thread 13hi this is thread  4hi this is thread 1hi this is thread 7hi this is  thread 12hi this is thread 18hi this is thread 19

A comparison of two examples shows that the difference lies in the output position of the "The last line", which is not modified before the code is at the end, and when the code is modified, it is explained as follows:

In the first example, the code of the program is stopped in the code block of I.join () because of the addition of join (), and there is no timeout set, so the program code behind the block is not started until all processes have been detected, and therefore, the "will be output at the last surface;

The second example does not add join () for detection, so no pipeline process has been completed, as long as all the function entry into the thread to start execution, immediately execute the I.start () code block after the program code, because the multithreaded execution of the function added sleep (1), So the output of the thread execution is certainly slower than printing "This was the last line" later, so this sentence will be output at the front.

Multithreading is so, multi-process is similar, there are already detailed examples and instructions.


2. Further explanations of the join ()

In fact, in the first example of the program code has given the explanation of join (), here only need to look at the following example is better understood.

The program code is as follows:

#!/usr/bin/env pythonimport threadingimport timedef sayhi (n): Time.sleep (2) print ' Hi This is thread%s '%nthread_list = []f or I in range (Ten): Thread_list.append (Threading. Thread (Target=sayhi, args= (i,)))) for I in Thread_list:i.start () for I in Thread_list:print I.join (0.1) print ' \033[32; 1mThis is the last line\033[0m '

The results of the program execution are as follows:

[Email protected]:/mnt/hgfs/python/day7$ time Python day6thread1.py Nonenonenonenonenonenonenonenonenonenonethis is The last Linehi this is a thread 0Hi this is a thread 1Hi this is a thread 2Hi this is a thread 3Hi this is thread 4Hi this is THR EAD 5 hi This is the thread 7 hi this is the thread 8Hi this is the thread 6Hi this is the thread 9real0m2.080suser0m0.048ssys0m0.016s

The following explanations are provided:

1) The program code to I.join (), because 0.1 seconds timeout, while the thread execution function sleep2 seconds;

2) After 0.1 seconds past, the first line blocks until those, I.join () detects the second thread, and the printout is none;

3) After 0.1 seconds past, the second line blocks until those, I.join () detects the third thread, and prints out none;

......

4) 10 threads were detected and 1 seconds were spent, when the work of Join () was completed (10 none in total), and the code block after join () was executed;

5) the code block after join () output "This is the last line";

6) After 1 seconds, all threads sleep, a total output of 10 sentence "Hi this is thread";

7) The program is completed and the process is finished.

Through the analysis of this example above, I believe that both the execution of multi-threaded and the join () function of understanding, there will be a further understanding.


This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1703322

"Python Tour" seventh (i): Another talk about Python multithreading

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.