Subprocess keeps open my port so I can't restart my app.

12 views
Skip to first unread message

sams...@gmail.com

unread,
Jun 13, 2008, 3:30:45 AM6/13/08
to TurboGears
There are a few things that the administrative part of my web site
needs to do. One of them involves starting a completely separate
python program. This separate program needs to run for 24 hours or
so. But once this program is started it's completely independent. It
never reports back to my Turbogears application.

Here's how I start it:

subprocess.Popen(command_line, shell=True, stdout=fd,
stderr=subprocess.STDOUT)

where command_line is the name of the program and a few command line
parameters.

Now this works pretty well. The subprocess continues after Turbogears
restarts itself (because I make a change in the code, or because I
restart it, etc.).

The problem is somehow or other the subprocess keeps the port open
that Turbogears was using. So I get this message:

File "/usr/lib/python2.5/site-packages/CherryPy-2.3.0-py2.5.egg/
cherrypy/_cpwsgiserver.py", line 370, in start
raise socket.error, msg
error: (98, 'Address already in use')

How can I start a process, and let it run after Turbogears has stopped
without it blocking the port Turbogears was using? The subprocess has
no reason to block said port. It does nothing with it.

Thanks

Diez B. Roggisch

unread,
Jun 14, 2008, 12:42:28 PM6/14/08
to turbo...@googlegroups.com
sams...@gmail.com schrieb:

I can only speculate on this because I don't have an idea why the
process is still holding a reference to the port. Actually, I have an
idea - but I don't know if it's correct :) I guess the child-process has
some "link" to the old TG process, preventing that from being collected
from the OS - and for some reason the socket isn't properly freed.

However, you might consider using the daemonize recipe in aspn python
cookbook to fork off another process that then will detatch itself from
the TG-process before invoking subprocess.

By that, you might get rid of the problem.

Another aleternative might be to not use the stdout and stderr-stuff, or
pointing these to /dev/null or such. They might otherwise form that
aforementioned "link"

Diez

Carlos Ble

unread,
Jul 11, 2008, 8:08:30 AM7/11/08
to TurboGears
Hi!
I had the same problem, not with turbogears but with the socket.
Should be the same.
Hope this helps: http://www.carlosble.com/?p=135
Just pass the command you want to run to the
runExternalCmdOnSeparateProcess method.
Reply all
Reply to author
Forward
0 new messages