Using the following example I can get a basic web server running but my problem is that the handle_request() blocks the do_something_else() until a request comes in. Is there any way around this to have the web server do other back ground tasks?
def run_while_true(server_class=BaseHTTPServer.HTTPServer,
handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
while keep_running():
httpd.handle_request()
do_something_else()
You can use multiple threads of execution through the Python threading module. An example is below:
This will cause a thread which executes
do_something_else()to start before your web server. When the server shuts down, thejoin()call ensuresdo_something_elsefinishes before the program exits.