How to use the Join function in Python multithreaded programming

Source: Internet
Author: User
Today to Xinji to buy bags, afternoon very late only to come back, but also nausea and headache. Nausea is because of bad food in the morning + Carsick + came back to see the scene of the accident, headache is probably the sun and air conditioning mixed stimulation. No time, no spirit, no strength to learn, this blog is about a small Python function.

Because of the pit father's professional, multi-threaded programming teacher never taught, multi-threaded concept is also taught, I Python is rookie level, so encountered multi-threading programming dumbfounded, others with the handy join function I just can't understand. In the morning on the way to Xinji think of this problem to think of nausea, back to continue to write code test, finally some understanding (Python official English explanation can not understand, the user's explanation is not enough detailed, only own drill).
The code for the test is as follows:
Copy the Code code as follows:


# Coding:utf-8

# Testing the functionality of joins in multi-threading

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 threads perform actions dowaiting () and DoWaiting1 (), the function body is print "Start"+ hibernate 3 seconds + print "End", respectively append time to view the process of program execution. The start () method synchronously starts executing two threads later. It then starts looping through the two-thread join () method, which is preceded and followed by the print function to start the end of the tag. We mainly observe for the TT in Tsk:tt.join ().

Join () without parameters, do the following:

As you can see, two threads are executed in parallel, process 1 ends after 3s, process 2 ends after 8s, and then goes back to the main process, performing a print "end join".

The following parameter is set to a timeout of 2s, i.e. tt.join (2), executed as follows:

Two threads begin to execute concurrently, then execute thread 1 join (2), wait for thread 1 to execute 2s after it, execute thread 2 of join (2), wait for thread 2 to execute 2s and then no matter it (in this process, the end of the thread 1 execution, the end of print threads 1), start to execute the main process, print "end join". Thread 2 execution ends after 4s.

To summarize:

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

2. In the case of multi-threaded multiple joins, the join method of each thread is executed sequentially, and the first one ends before the next one can be executed.

3. Without parameters, wait until the thread ends before the next thread's join IS started.

4. After setting the parameter, wait for the thread to ignore it for such a long time (and the thread does not end). Whatever it means is that you can execute the following main process.

Finally, the program execution flow table with parameter 2 o'clock is attached, and the Orz is drawn, so it seems better to understand.

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