Printing in parallel processes using Python sub-process module

utengr Source

I am using python subprocesses as follows.

import subprocess


p1 = subprocess.Popen(['python', 'test1.py'])
p2 = subprocess.Popen(['python', 'test2.py'])

p1.wait()
p2.wait()

print('The main and sub-processes are finished')

There is a print statement in both test1.py and test2.py. Its desired behavior is to print 'test1' or 'test2' continuously.

I was expecting a mingled kind of output as they are running in parallel. However, I am just seeing only 'test1':

test1
test1
test1
and so on. 

What am I missing here?

Desired output: Both print statements should be on STDOUT.

NOTE: This a very simplified example of the problem. NOTE: Imagine the test1 and test2 looks like this.

test1.py:

for i in range(100000):
    print('test1')

test2.py:

for i in range(100000):
    print('test2')
python-3.xmultiprocessingsubprocess

Answers

answered 3 months ago Hannu #1

Your program works fine. You just get a lot of trailing output from one of the processes as the busy loops do not run in tandem. Try changing your test1 and test2 to this:

from time import sleep

for i in range(1000):
    print('test2')
    sleep(0.1)

And you will see your expected output.

comments powered by Disqus