Last active
February 28, 2016 10:27
-
-
Save omsobliga/54922e86df976af9a727 to your computer and use it in GitHub Desktop.
Child thread with join, main thread wait unit all child threads terminate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" Child threads with join | |
Main thread wait unit all child threads terminate. | |
""" | |
import threading | |
from time import sleep | |
def fun1(arg): | |
for i in xrange(arg): | |
print 'Thread1 ID={}'.format(threading.current_thread()) | |
sleep(1) | |
def fun2(arg): | |
for i in xrange(arg): | |
print 'Thread2 ID={}'.format(threading.current_thread()) | |
sleep(1) | |
if __name__ == "__main__": | |
print 'Start...' | |
# Print thread ID. | |
print 'Main thread ID={}'.format(threading.current_thread()) | |
thread1 = threading.Thread(target=fun1, args=(3, )) | |
thread2 = threading.Thread(target=fun2, args=(3, )) | |
thread1.start() | |
thread2.start() | |
# Make sure main thread run after all clildren thread run over. | |
thread1.join() | |
thread2.join() | |
print 'End...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment