Kodi Community Forum
How to read stderr=PIPE in real time? - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=26)
+--- Thread: How to read stderr=PIPE in real time? (/showthread.php?tid=94028)



How to read stderr=PIPE in real time? - Popeye - 2011-02-13

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?


- bobo1on1 - 2011-02-13

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.