Managing a multiple threaded service

Paul Hemans darwin at nowhere.com
Wed Jun 17 02:23:53 EDT 2009


Hi,
New to Python ....
I've got 2 threads 1 is the SimpleHTTPRequestHandler, the other polls a site 
for data. I want to run the program as a windows service.
My questions are:
Should both of them run as threads and then just have an infinite loop with 
a sleep in the main thread in order to stop the main program from just 
terminating?
Or should I choose one of the threads to run as the main program and spawn 
the other off? That was my original intention, but I am concerned that if I 
use SimpleHTTPRequestHandler in the main thread, will it block any attempt 
to terminate the service.

Just looking for pointers to the best practice approach. This is where I am 
at:

if __name__=="__main__":
    import SocketServer
    import threading
    import monitor

    ## Monitor changes on the server (separate thread)
    monitor = monitor.monitor(fnLog=self.logStatus)
    monitor.start()

    ## Service interface
    port = 8081
    server=SocketServer.TCPServer(('',port),ScriptRequestHandler)
    server.serve_forever()
    ## To run the web server on a different thread...
##    t = threading.Thread(target=server.serve_forever)
##    t.setDaemon(True) # don't hang on exit
##    t.start()

    server.socket.close()






More information about the Python-list mailing list