How to read stderr=PIPE in real time?
#1
Question 
I'm writing a add-on for displaying status of downloads. The downloads are executed in a python script keeping queue data in a sqlite data base. My problem is the actual download are done by binary writing its status on stderr and it takes minutes / hours to finish.

Code:
dmp = subprocess.Popen(args, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                while True:
                    if dmp.poll() is not None:
                        break
                    while True:
                        line = dmp.stderr.readline()
                        if not line:
                            break
                        print  line.rstrip()
                    time.sleep(5)
                print "Dumping done!"
Using readline() will block if there is a error in the download. Using subprocess.communicate() will only diplay output once execution is done
Code:
dmp = subprocess.Popen(args, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                while True:
                    if dmp.poll() is not None:
                        break
                    (stdout, stderr) = dmp.communicate()
                    print "out: " + stdout + " error: " + stderr
                    time.sleep(5)
                print "Dumping done!"

I could of course write stderr to a file and read from this but if I could avoid it it would be nice/cleaner

I've googled for hours and read a good amount of similar problems on stackoverflow but not got any clear answers. Any of you skilled coders got any suggestions on how to proced?
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#2
In C you would get the file descriptor from the FILE*, then do either a non blocking read on that, or use select() with a timeout.
I'm not sure if python exposes such functionality.

Maybe you can just call the C functions from python instead.
Reply

Logout Mark Read Team Forum Stats Members Help
How to read stderr=PIPE in real time?0