2011-02-13, 01:10
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.
Using readline() will block if there is a error in the download. Using subprocess.communicate() will only diplay output once execution is 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?
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!"
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?