Connecting Tech Pros Worldwide Forums | Help | Site Map

subprocess seems to "detach" / ignore wait()

Mathieu Prevot
Guest
 
Posts: n/a
#1: Aug 20 '08
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)

Wojtek Walczak
Guest
 
Posts: n/a
#2: Aug 20 '08

re: subprocess seems to "detach" / ignore wait()


On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
Quote:
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/
Gabriel Genellina
Guest
 
Posts: n/a
#3: Aug 20 '08

re: subprocess seems to "detach" / ignore wait()


En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick@bzt.bztescribió:
Quote:
On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
>
Quote:
> 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

Mathieu Prevot
Guest
 
Posts: n/a
#4: Aug 21 '08

re: subprocess seems to "detach" / ignore wait()


2008/8/20 Gabriel Genellina <gagsl-py2@yahoo.com.ar>:
Quote:
En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick@bzt.bztescribió:
>
Quote:
>On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
>>
Quote:
>> 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
Mathieu Prevot
Guest
 
Posts: n/a
#5: Aug 21 '08

re: subprocess seems to "detach" / ignore wait()


2008/8/21 Mathieu Prevot <mathieu.prevot@ens.fr>:
Quote:
2008/8/20 Gabriel Genellina <gagsl-py2@yahoo.com.ar>:
Quote:
>En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick@bzt.bztescribió:
>>
Quote:
>>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
Gabriel Genellina
Guest
 
Posts: n/a
#6: Aug 21 '08

re: subprocess seems to "detach" / ignore wait()


En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <mathieu.prevot@ens.frescribió:
Quote:
Quote:
>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))
Quote:
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

Mathieu Prevot
Guest
 
Posts: n/a
#7: Aug 21 '08

re: subprocess seems to "detach" / ignore wait()


2008/8/21 Gabriel Genellina <gagsl-py2@yahoo.com.ar>:
Quote:
En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <mathieu.prevot@ens.frescribió:
>
Quote:
Quote:
>>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))
>
Quote:
>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
Closed Thread