Experience in using join functions in python multi-thread programming

Source: Internet
Author: User
This article mainly introduces how to use the join function in python multi-threaded programming. This article first provides examples of using the join function and summarizes the use of the join function, if you need it, you can refer to the next day to buy bags in Xinji. it will be back late in the afternoon. it is 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:

The 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.

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.