Make two child processes do 10 seconds of work each in parallel, reporting their output to stdout in an interleaved fashion:
$ python parent.py
child 1 reporting for work
child 2 reporting for work
child 1 done working
child 2 done working
import time | |
if __name__ == '__main__': | |
print('child 1 reporting for work') | |
time.sleep(10) | |
print('child 1 done working') |
import time | |
if __name__ == '__main__': | |
print('child 2 reporting for work') | |
time.sleep(10) | |
print('child 2 done working') |
import subprocess | |
if __name__ == '__main__': | |
c1 = subprocess.Popen('python child1.py', shell=True) | |
c2 = subprocess.Popen('python child2.py', shell=True) | |
c1.wait() | |
c2.wait() |