Python Forum

Full Version: Multithreaded HTTP Server
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a simple HTTP server that processed data request from multiple clients. I'm using Windows Python 3.8 and ThreadedHTTPServer, but that appears to only use threads to accept requests, but don't actually spawn a new thread to process the request. So in this example below, when a request is for doLargeTask(), the server hangs until it finishes the large request, while no other requests are processed.

Many thanks for some guidance.

from http.server import HTTPServer, BaseHTTPRequestHandler, ThreadingHTTPServer
from socketserver import ThreadingMixIn

class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
    pass

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
	def do_GET(self):
        
		self.send_response(200)
		self.end_headers()
        
          if A: 
		resp = doSmallTask()
                resp = resp.encode('utf-8')
                self.wfile.write(resp)
          if B:
		resp = doLargeTask()
                resp = resp.encode('utf-8')
                self.wfile.write(resp)


	 
httpd = ThreadingHTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()