469,362 Members | 2,441 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

subprocess seems to "detach" / ignore wait()

Hi there,

it seems that child.wait() is ignored when

print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)

are between the process creation child = Popen(cmd.split(),
stderr=flog) and child.wait().
It seems to be a bug, doesn't it ?

Mathieu

(I'm running x11vnv with args in the cmd string on FreeBSD 8.0/CURRENT)

flog = open(logfile, 'w')
fpid = open(pidfile, 'w')
try:
child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)
child.wait()
except KeyboardInterrupt:
print "INT sent to vnc server"
finally:
fpid.close()
flog.close()
os.remove(pidfile)
os.remove(logfile)
sys.exit(0)
Aug 20 '08 #1
6 2131
On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
flog = open(logfile, 'w')
fpid = open(pidfile, 'w')
try:
child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)
What happens if you change:

fpid.write(child.pid)

into:

fpid.write('%d\n' % (child.pid))

I think that the problem here is that fpid.write() fails silently
(probably TypeError), because it takes string as its first argument,
not integer.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Aug 20 '08 #2
En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gm*****@bzt.bztescribiˇ:
On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
> child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)

I think that the problem here is that fpid.write() fails silently
(probably TypeError), because it takes string as its first argument,
not integer.
Exactly, but it doesn't fail "silently" (that would be a bug). The exception is raised, but due to the finally clause ending in sys.exit(0), it has no chance of being handled.
This is the original code, for reference:

flog = open(logfile, 'w')
fpid = open(pidfile, 'w')
try:
child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)
child.wait()
except KeyboardInterrupt:
print "INT sent to vnc server"
finally:
fpid.close()
flog.close()
os.remove(pidfile)
os.remove(logfile)
sys.exit(0)

--
Gabriel Genellina

Aug 20 '08 #3
2008/8/20 Gabriel Genellina <ga*******@yahoo.com.ar>:
En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gm*****@bzt.bztescribiˇ:
>On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
>> child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)

I think that the problem here is that fpid.write() fails silently
(probably TypeError), because it takes string as its first argument,
not integer.

Exactly, but it doesn't fail "silently" (that would be a bug). The exception is raised, but due to the finally clause ending in sys.exit(0), it has no chance of being handled.
This is the original code, for reference:

flog = open(logfile, 'w')
fpid = open(pidfile, 'w')
try:
child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)
child.wait()
except KeyboardInterrupt:
print "INT sent to vnc server"
finally:
fpid.close()
flog.close()
os.remove(pidfile)
os.remove(logfile)
sys.exit(0)

--
Gabriel Genellina

Indeed, I got TypeError: argument 1 must be string or read-only
character buffer, not int
and Wojtek's code works. So what is the right thing to do so my script
returns 1 or 0 depending on its state and success ?

Mathieu
Aug 21 '08 #4
2008/8/21 Mathieu Prevot <ma************@ens.fr>:
2008/8/20 Gabriel Genellina <ga*******@yahoo.com.ar>:
>En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gm*****@bzt.bztescribiˇ:
>>On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:

child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)

I think that the problem here is that fpid.write() fails silently
(probably TypeError), because it takes string as its first argument,
not integer.

Exactly, but it doesn't fail "silently" (that would be a bug). The exception is raised, but due to the finally clause ending in sys.exit(0), it hasno chance of being handled.
This is the original code, for reference:

flog = open(logfile, 'w')
fpid = open(pidfile, 'w')
try:
child = Popen(cmd.split(), stderr=flog)
print "Server running [PID %s]"%(child.pid)
fpid.write(child.pid)
child.wait()
except KeyboardInterrupt:
print "INT sent to vnc server"
finally:
fpid.close()
flog.close()
os.remove(pidfile)
os.remove(logfile)
sys.exit(0)

--
Gabriel Genellina


Indeed, I got TypeError: argument 1 must be string or read-only
character buffer, not int
and Wojtek's code works. So what is the right thing to do so my script
returns 1 or 0 depending on its state and success ?
PS: BTW how can I detach my process ie have an equivalent to
`myscript.py&` from the python script ?

Thanks,
Mathieu
Aug 21 '08 #5
En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <ma************@ens.frescribiˇ:
>So what is the right thing to do so my script
returns 1 or 0 depending on its state and success ?
I use something like this:

def main(argv):
try:
try:
do_things()
return 0
finally:
do_cleanup()
except:
log_exception()
return 1

if __name__=='__main__':
import sys
sys.exit(main(sys.argv))
PS: BTW how can I detach my process ie have an equivalent to
`myscript.py&` from the python script ?
There are a few recipes in the Python CookBook at http://code.activestate.com/recipes/langs/python/

--
Gabriel Genellina

Aug 21 '08 #6
2008/8/21 Gabriel Genellina <ga*******@yahoo.com.ar>:
En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <ma************@ens.frescribiˇ:
>>So what is the right thing to do so my script
returns 1 or 0 depending on its state and success ?

I use something like this:

def main(argv):
try:
try:
do_things()
return 0
finally:
do_cleanup()
except:
log_exception()
return 1

if __name__=='__main__':
import sys
sys.exit(main(sys.argv))
>PS: BTW how can I detach my process ie have an equivalent to
`myscript.py&` from the python script ?

There are a few recipes in the Python CookBook at http://code.activestate..com/recipes/langs/python/
The return from main()... it was my thought too.
Thank you Gabriel :)

Mathieu
Aug 21 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dmitri | last post: by
1 post views Thread by Sean Nolan | last post: by
5 posts views Thread by Jeremy | last post: by
4 posts views Thread by Gianni Mariani | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.