Experience in using the join function in python multi-thread programming, and using python multi-thread programming

Source: Internet
Author: User

Experience in using the join function in python multi-thread programming, and using python multi-thread programming

Today, I went to Xinji to buy my bags and came back late in the afternoon. It was disgusting and a headache. It's disgusting because I saw a car accident at the scene when I came back from eating bad things in the morning + dizzy cars. The headache was probably caused by a mix of hot sun and air conditioners. There is no time, no spirit, no strength to learn. This blog will talk about a small function in python.

Due to the expertise of the students, the multi-thread programming teacher has never taught, and the concept of multi-thread is also confusing. I am also at the cainiao level, so I am dumpfounded when I encounter multi-thread programming, I can't understand the join functions that others use. I thought of this problem on the way to Xinji in the morning and thought it was disgusting. When I came back to continue writing code for testing, I finally understood it. (the official explanation of python cannot be understood in English, and the explanation of netizens is not detailed enough, only drill by yourself ).
The code used for testing is as follows:
Copy codeThe Code is as follows:
# Coding: UTF-8

# Testing the join function in multiple threads

Import threading, time
Def doWaiting ():
Print 'start waiting1: '+ time. strftime (' % H: % M: % s') + "\ n"
Time. sleep (3)
Print 'Stop waiting1: '+ time. strftime (' % H: % M: % s') + "\ n"
Def doWaiting1 ():
Print 'start waiting2: '+ time. strftime (' % H: % M: % s') + "\ n"
Time. sleep (8)
Print 'Stop waiting2: ', time. strftime (' % H: % M: % s') + "\ n"
Tsk = []
Thread1 = threading. Thread (target = doWaiting)
Thread1.start ()
Tsk. append (thread1)
Thread2 = threading. Thread (target = doWaiting1)
Thread2.start ()
Tsk. append (thread2)
Print 'start join: '+ time. strftime (' % H: % M: % s') + "\ n"
For tt in tsk:
Tt. join ()
Print 'end join: '+ time. strftime (' % H: % M: % s') + "\ n"

This applet uses two threads, thread1 and thread2. The actions executed by the thread are respectively doWaiting () and doWaiting1 (), the function body prints start + sleep for 3 seconds + prints end, and attaches the time to view the program execution process. The start () method is used to synchronize the execution of two threads. Then start to call the join () method of the two threads cyclically, and mark the start and end with the print function before and after this. We mainly observe for tt in tsk: tt. join ().

If join () does not contain parameters, run the following command:

We can see that two threads run in parallel, process 1 ends after 3 s, process 2 ends after 8 s, and then return to the main process and execute the print "end join 」.

The following parameter is set to timeout 2 s, that is, tt. join (2). the execution is as follows:

The two threads start concurrent execution, and then execute join (2) of thread 1. After thread 1 is executed for 2 seconds, the join (2) of thread 2 is executed ), after thread 2 is executed for 2 seconds, it does not matter (in this process, thread 1 is executed and the end information of thread 1 is printed). The main process is executed and "end join" is printed 」. Thread 2 is finished after 4s.

Summary:

1. The function of the join method is to block the main process (blocking, unable to execute statements after join) and focus on executing multithreading.

2. In the case of multi-threaded and multi-join, the join methods of each thread are executed in sequence, and the first one is finished before the execution is followed.

3. If no parameter exists, the join of the next thread starts to be executed until the end of the thread.

4. After the parameter is set, wait for the thread to ignore it for so long (and the thread has not ended ). No matter what it means, you can execute the following main process.

Finally, the program execution flow table with the parameter 2 is attached, and the self-drawn orz is better understood.


What is the use of the join () function in python? Hope to answer our little white

Connect all strings in a list according to the separator you define, for example:
List = ['A', 'B', 'C']
Sep = '|'
The result of join (list, sep) is a | B | c

Python Multithreading

The join operation is to block the main thread. When the main thread stops, it will not continue until all the sub-threads exit.
By default, once the main thread exits, the program is terminated. To ensure that other threads complete the task, the main thread must be blocked and the other threads are finished before proceeding to the next step.

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.