Slow termination of process

Roland Hedberg roland at
Tue Mar 27 15:03:11 CEST 2012


I have an application/a script that is run from another application.
The script is mostly working as a HTTP client but in some cases it also has to act as a HTTP server.
Basically just for serving a few files. The files are dynamically created by the script when needed.

To accomplish this I'm trying to use subprocess Popen
and an extremely simple web server script which basically contains:

    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    httpd = SocketServer.TCPServer((hostname, port), Handler)

In the main script I do:

    op = Popen(popen_args, stdout=PIPE, stderr=PIPE)

When the main script is done it closes down the HTTP server by doing:


The problem I have is that if the main script is run again almost immediate then the old HTTP server 
process doesn't seem to have released the port yet. So setting up a new server fails.

Is there anything I can do to get the port released immediately when the tcpserver is terminated.
Or is there another way of doing this that will work better ?


With anchovies there is no common ground 
-- Nero Wolfe

More information about the Python-list mailing list