You can use multiple threads of execution through the Python streaming module . The following is an example:
import threading
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()
if __name__ == '__main__':
background_thread = threading.Thread(target=do_something_else)
background_thread.start()
background_thread.join()
This will trigger a thread that starts do_something_else()to run in front of your web server. When the server shuts down, the call join()terminates do_something_elsebefore the program exits.
source
share