473,480 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2304
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3476
by: Gavin Williams | last post by:
I am working on a multi-threaded server for a Windows 2000 system and since "fork" doesn't work that great in a Win32 environment, I am trying to use use the "threads" module instead. When a...
6
4374
by: Dmitri | last post by:
Hi there, Does anybody know what is DB2 UDB admin API equivalent to "db2 terminate" command? Some background: I'm developing monitoring application(http://chuzhoi_files.tripod.com). I want...
1
3202
by: Sean Nolan | last post by:
We have implemented unhandled error trapping at the application level and log these errors to our database. One error, however, the does not get trapped is when the connection pool has exceeded the...
28
7330
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
4
5013
by: Joey | last post by:
Hi, I wrote a mixed-mode dll (with MFC and C++/CLI) which is called from a C#-EXE. Under special cirumstances (that is: another process sends a windows-message to my process - this message is...
3
2174
by: Pappy | last post by:
SHORT VERSION: Python File B changes sys.stdout to a file so all 'prints' are written to the file. Python file A launches python file B with os.popen("./B 2>&^1 >dev/null &"). Python B's output...
5
14409
by: Glen Buell | last post by:
Hi all, I have a major problem with my ASP.NET website and it's SQL Server 2005 Express database, and I'm wondering if anyone could help me out with it. This site is on a webhost...
5
2212
by: Jeremy | last post by:
Is there any good reading about pitfalls of scoping when using events? Here is my specific issue: function MyType() { this.foo = "bar"; this.textbox = document.createElement("input");...
4
4648
by: Gianni Mariani | last post by:
Two issues here: a) What is the accepted definition of "observer pattern". While I can't point to anything specific, I do remember having issues with inconsistency in the definition. b)...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.