subprocess seems to "detach" / ignore wait()

Mathieu Prevot mathieu.prevot at ens.fr
Thu Aug 21 07:41:54 CEST 2008


2008/8/20 Gabriel Genellina <gagsl-py2 at yahoo.com.ar>:
> En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick at bzt.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



More information about the Python-list mailing list