Managing a multiple threaded service

P

Paul Hemans

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()
 
M

Mark Hammond

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?

No need for a loop - just have the main thread wait forever for the stop
request. The main problem I forsee is asking the socketserver thread to
terminate as it is likely to be inside 'accept' or some other blocking
function.

HTH,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top