Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popen stdout reading pipe, deadlock using sleep

Well, I have two scripts. The a.py which prints the output of the b.py script as follows:

#a.py
from subprocess import Popen, PIPE, STDOUT

p = Popen(['/Users/damian/Desktop/b.py'], shell=False, stdout=PIPE, stderr=STDOUT)

while p.poll() is None:
    print p.stdout.readline()


#b.py
#!/usr/bin/env python
import time

while 1:
    print 'some output'
    #time.sleep(1)

This works.But, Why do my scripts deadlock when I uncomment the time.sleep() line?

like image 376
ScotchAndSoda Avatar asked Mar 15 '26 01:03

ScotchAndSoda


1 Answers

Your output is probably buffered. Add a .flush() for stdout to clear it:

import sys
import time

while 1:
    print 'someoutput'
    sys.stdout.flush()
    time.sleep(1)
like image 82
Martijn Pieters Avatar answered Mar 17 '26 20:03

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!