The use of Join function in Python multithreaded programming _python

Source: Internet
Author: User
Tags in python

Today to Xinji to buy bags, late afternoon to come back, but also nausea and headache. Nausea is because the morning eat bad + Carsick + back when you see the accident scene, the headache is probably the hot sun and air conditioning mixed stimulation. There is no time, no spirit, no strength to learn, this blog is about a small function in Python.

Because pit Dad's school pit ye Professional, multithreaded programming teacher never taught, multithreading concept is also taught confusedly, I Python is a rookie level, so I encountered multithreading programming is dumbfounded, others use the convenient join function I just can't understand. In the morning on the way to Xinji think of this problem think of nausea, back to continue to write code test, finally some understand (Python official English explanation can not understand, the user's explanation is not detailed enough, can only own drill).
The code for the test is as follows:

Copy Code code as follows:

# Coding:utf-8

# test the function of join 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 thread executes the action is dowaiting () and DoWaiting1 (), the function body is print "Start"+ hibernate 3 seconds + print "End", attach time to view the process of program execution respectively. The following starts with the start () method to execute two threads. Then start iterating over the join () method of the two threads, 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 execute in parallel, process 1 ends after 3s, process 2 ends at 8s, then goes back to the main process, performing print "end join".

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

Two threads start concurrent execution, then execute the join of thread 1 (2), wait for thread 1 to execute 2s after it, execute thread 2 's join (2), wait for thread 2 to execute 2s after it also ignore it (in this process thread 1 execution end, print threads 1 end information), start execution of main process, print "end join". Thread 2 execution ends after 4s.

To sum up:

The 1.join method is to block the main process (blocking, unable to execute the statement after join), focus on the implementation of multithreading.

2. Multi-threaded join in the case, sequentially executes the join method of each thread, the front end of a to perform the following.

3. No parameters, wait until the end of the thread to begin executing the next thread join.

4. After the parameter is set, the thread waits for such a long time to ignore it (and the thread does not end). Whatever the meaning is, you can perform the following main process.

Finally, attach the program execution flow chart with the parameter 2 o'clock, and draw your own orz so that it looks 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.