I'm using python 2.5.2 or 2.7 which is a HTTP server (BaseHTTPServer) that launches various tasks. One of the processes is a long running process. What I would like to be able to do is to launch this process then close my HTTP server and restart.
The problem is that my server shuts down (closes all threads and the python.exe process goes out of the active task list shown by Windows, the launched process is still running, but netstat -ab shows that the sytem process has my port the HTTP server listens on in a LISTENING state and associated to the Process ID which used to be my HTTP server. That port is kept open until the launched process completes, which makes it impossible to restart my HTTP server.
Whether I kill the python process, or CTRL-C the window, the same behavior is exhibited. I've read a ton of documentation and everyone suggests using subprocess.Popen, but even using that seems to associate parts of the main process to the launched process.
I'm launching the utility as follows:
try:
# NOTE: subprocess.Popen is hanging up the 8091 port until the utility finishes.
# This needs to be addressed, otherwise, I'll never be able to restart the
# client when the utility has been launched.
listParams = [ 'C:/MyPath/My.exe', '-f', os.path.join ( sXMLDir, sXmlFile ) ]
proc = subprocess.Popen ( listParams, cwd='C:/MyPath', creationflags=0x00000008 )
iSts = 200
sStatus = 'Utility was successfully launched.'
except:
iSts = CMClasses.HTTPSTS_STARTSLEDGE_SYSTEM
sStatus = 'An exception occurred launching utility: ' + str ( sys.exc_type ) + ":" + str ( sys.exc_value ) + '.'
My HTTP server is implemented as follows which allows my main program to process a CTRL-C:
class LaunchHTTPServer ( Thread ):
def __init__ ( self, sPort, CMRequestHandler ):
Thread.__init__ ( self )
self.notifyWindow = None
self.Port = sPort
self.CMRequestHandler = CMRequestHandler
self.bExecute = True
def run ( self ):
server = stoppableHttpServer(('',self.Port), self.CMRequestHandler )
server.serve_forever()
server.socket.close()
def getExecute ( self ):
return ( self.bExecute )
def endThread ( self ):
pass
class stoppableHttpServer ( BaseHTTPServer.HTTPServer ):
def serve_forever ( self ):
self.stop = False
while not self.stop:
self.handle_request()
def main ( argv ):
...
try:
....
tLaunchHTTPServer = LaunchHTTPServer ( iCMClientPort, CMRequestHandler )
tLaunchHTTPServer.start()
...
except KeyboardInterrupt:
logging.info ( 'main: Request to stop received' )
# End the communication threads
logging.info ( 'Requesting CMRequestHandler to close.' )
conn = httplib.HTTPConnection ( "localhost:%d" % iCMClientPort )
conn.request ( "QUIT", "/" )
conn.getresponse()
conn.close()
Here are the results from the netstat -ab (my python process is 3728, my port is 8091) before starting the utility:
Active Connections
Proto Local Address Foreign Address State PID
TCP vtxshm-po-0101:8091 vtxshm-po-0101:0 LISTENING 3728 [python.exe]
TCP vtxshm-po-0101:8091 vtxshm-po-0101:23193 TIME_WAIT 0 [FrameworkService.exe]
Here are the results from the netstat -ab after starting the utility and after hitting Control-C and having python stop. (note that the OS thinks that this port is still in a LISTENING state, assigned to PID 3728, but that process no longer exists in Task Manager and this is now owned by System and somehow related to snmp.exe (which we don't even use) ). These connections are understood as they are the requests from another server to start the utility.
Active Connections
Proto Local Address Foreign Address State PID
TCP vtxshm-po-0101:8091 vtxshm-po-0101:0 LISTENING 3728 [System]
TCP vtxshm-po-0101:8091 CH2ChaosMonkeyServer:2133 TIME_WAIT 0 TCP vtxshm-po-0101:8091 CH2ChaosMonkeyServer:2134 TIME_WAIT 0 TCP vtxshm-po-0101:8091 vtxshm-po-0101:23223 TIME_WAIT 0 [snmp.exe]
Has anyone successfully launched a process from python and completely had it run independently from the launching process? If so, could you please share the secret?