Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

subprocess seems to "detach" / ignore wait()

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


mathieu.prevot at gmail

Aug 20, 2008, 6:09 AM

Post #1 of 7 (290 views)
Permalink
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)
--
http://mail.python.org/mailman/listinfo/python-list


gminick at bzt

Aug 20, 2008, 8:22 AM

Post #2 of 7 (272 views)
Permalink
Re: subprocess seems to "detach" / ignore wait() [In reply to]

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/
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py2 at yahoo

Aug 20, 2008, 1:22 PM

Post #3 of 7 (251 views)
Permalink
Re: subprocess seems to "detach" / ignore wait() [In reply to]

En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick [at] bzt> escribió:

> 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

--
http://mail.python.org/mailman/listinfo/python-list


mathieu.prevot at ens

Aug 20, 2008, 10:41 PM

Post #4 of 7 (243 views)
Permalink
Re: subprocess seems to "detach" / ignore wait() [In reply to]

2008/8/20 Gabriel Genellina <gagsl-py2 [at] yahoo>:
> En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick [at] bzt> escribió:
>
>> 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
--
http://mail.python.org/mailman/listinfo/python-list


mathieu.prevot at ens

Aug 20, 2008, 10:46 PM

Post #5 of 7 (241 views)
Permalink
Re: subprocess seems to "detach" / ignore wait() [In reply to]

2008/8/21 Mathieu Prevot <mathieu.prevot [at] ens>:
> 2008/8/20 Gabriel Genellina <gagsl-py2 [at] yahoo>:
>> En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick [at] bzt> escribió:
>>
>>> 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 ?

PS: BTW how can I detach my process ie have an equivalent to
`myscript.py&` from the python script ?

Thanks,
Mathieu
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py2 at yahoo

Aug 21, 2008, 12:26 AM

Post #6 of 7 (244 views)
Permalink
Re: subprocess seems to "detach" / ignore wait() [In reply to]

En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <mathieu.prevot [at] ens> escribió:

>> 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

--
http://mail.python.org/mailman/listinfo/python-list


mathieu.prevot at ens

Aug 21, 2008, 2:17 AM

Post #7 of 7 (241 views)
Permalink
Re: subprocess seems to "detach" / ignore wait() [In reply to]

2008/8/21 Gabriel Genellina <gagsl-py2 [at] yahoo>:
> En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <mathieu.prevot [at] ens> escribió:
>
>>> 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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.