Skip to content

Instantly share code, notes, and snippets.

@omsobliga
Last active February 28, 2016 10:27
Show Gist options
  • Save omsobliga/54922e86df976af9a727 to your computer and use it in GitHub Desktop.
Save omsobliga/54922e86df976af9a727 to your computer and use it in GitHub Desktop.
Child thread with join, main thread wait unit all child threads terminate.
#!/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