Skip to content

Instantly share code, notes, and snippets.

@thomasgilgenast
Last active March 15, 2017 17:59
Show Gist options
  • Save thomasgilgenast/b967d69621292a99c401132a2c77ef8e to your computer and use it in GitHub Desktop.
Save thomasgilgenast/b967d69621292a99c401132a2c77ef8e to your computer and use it in GitHub Desktop.
parallel python subprocesses with interleaved writing to stdout
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')

Usage

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 subprocess
if __name__ == '__main__':
c1 = subprocess.Popen('python child1.py', shell=True)
c2 = subprocess.Popen('python child2.py', shell=True)
c1.wait()
c2.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment