TCP servers in Python - two processes want to use same port

Pif Paf p1fp4f at hotmail.com
Tue Feb 24 07:22:09 EST 2004


Peter Hansen <peter at engcorp.com> wrote in message news:<403A248D.47B51ECE at engcorp.com>...
> Pif Paf wrote:
> > 
> > The child process is a very simple program using SimpleHTTPServer. The
> > problem is when I run my test harness, break it with Ctrl-C then try to
> > run it again. The http server complains that it cannot bind to the socket,
> > as the address is already in use.
> 
> I believe you should be looking at SO_REUSE_ADDR as a means of rebinding
> to a socket that was just used recently and may still be active at the 
> OS level, even if the app that used it has exited.

I will give that a go.

> As for the specific question about ways to kill the app that is listening
> on a socket already: I think if you don't know which app it is, there
> isn't a clean way to find out. 

Clearly the operating system knows, so there should be a way to find out.

> If you do know, though, as your test 
> harness probably does, can't it just "kill -9" the thing?

The test harness typically does know the process. However, if the test 
harness stops without doing proper cleanup (e.g. deleting a child processes 
it has created), then when it is restarted there will be "debris" from
the last run that is stopping it from running properly. This is not,
perhaps, a big issue when it's just the test harness, but it is a major 
issue on the production system. If the production system goes down for
any reason, I want to be able to bring it back up again by typing one 
command from the command line. This will run the master process, which in 
turn will restart other processes, and it is not acceptable for an old 
process to be clogging up a socket. 

-- 
Pif Paf



More information about the Python-list mailing list