Python Web Server - Getting it to do other tasks -
Using the following example I am running a basic web server, but my problem is that handle_request () blocks do_something_else ( ) Until no request comes. Does the web server need to do any other work back around it? Def run_while_true (server_class = BaseHTTserver.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 thread executions through Python Are there. The example is given below:
import threading # ... your code is here ... def run_while_true (server_class = BaseHTTPServer.HTTPServer, handler class = base.httpreserve.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 () # ... Web Server start code here ... background_thread.join ()
This is the reason for a thread executing do_something_else ()
to start your web server first Will be sir God gets closed, then join ()
ensures that do_something_else
finished the program before the end of the call.
Comments
Post a Comment