popen3 with python
Maric
maric.michaud at cirec.com
Wed Mar 20 12:01:33 EST 2002
That's it i think.
here my piece of code to prevent it and scan every char on either stdin or
stdout
raccroche = select.poll()
stdout_upd = select.poll()
stderr_upd = select.poll()
# Lancement de la commande
pipe_out = os.popen3(tempCmd)
stdin, stdout, stderr = pipe_out
for i in pipe_out :
raccroche.register(i.fileno(), select.POLLHUP)
stdout_upd.register(stdout, select.POLLIN)
stderr_upd.register(stderr, select.POLLIN)
s, e = (None,)*2
s_looping, e_looping = (0,)*2
# tant que le processus fils n'a pas raccroché ou qu'il ne s'est pas endormi
!
while(s_looping or e_looping or not raccroche.poll(0)):
events.clear()
while(s!='' and stdout_upd.poll(0)) :
s = os.read(stdout.fileno(),1)
if s == '' : break
# do something with s
while(e!='' and stderr_upd.poll(0)) :
e = os.read(stderr.fileno(),1)
if e == '' : break
# do something with e
# on empêche au processus fils de s'endormir
if s_looping :
self.exitStatus = stdout.close()
break
if s == '' : s_looping = 1
if e_looping :
stderr.close()
break
if e == '' : e_looping = 1
# Récupération des informations utiles
if not stdout.closed : self.exitStatus = stdout.close()
if not stderr.closed : stderr.close()
if self.exitStatus is None : self.exitStatus = 0
----Message d'origine-----
De : Thomas Guettler [mailto:guettli at thomas-guettler.de]
Envoyé : dimanche 10 mars 2002 23:55
À : Maric MICHAUD
Objet : popen3 with python
Hi Maric!
You replied to my question. I found the answer, maybe you have the
same deadlock condition:
If there is a lot of output on e.g. stderr and you start reading
from stdin you have a deadlock
Example:
The subprocess writes a lot of data to stderr and blocks.
In your application you use:
(stdin, stdout, stderr)=popen3("command_with_a_lot_of_output")
stdout.read()
--> deadlock
thomas
--
Thomas Guettler <guettli at thomas-guettler.de>
http://www.thomas-guettler.de
More information about the Python-list
mailing list