BasicHTTPServer, SimpleHTTPServer and concurrency

I am writing a small web server for testing using python, BasicHTTPServer and SimpleHTTPServer. It looks like it is processing one request at a time. Is there a way to do this a little faster without cheating too much? Basically my code is as follows, and I would like it to be simple :)

os.chdir(webroot) httpd = BaseHTTPServer.HTTPServer(("", port), SimpleHTTPServer.SimpleHTTPRequestHandler) print("Serving directory %s on port %i" %(webroot, port) ) try: httpd.serve_forever() except KeyboardInterrupt: print("Server stopped.") 
+4
source share
3 answers

You can create your own thread or forking class with mixin inheritance from SocketServer :

 import SocketServer import BaseHTTPServer class ThreadingHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): pass 

This has its limits as it does not use a thread pool, is limited to GIT, etc., but it can help a little (with relatively little effort). Remember that requests will be served by several threads at the same time, so be sure to lock around access to global / shared data (except when such data is unchanged after launch) during the service of the request.

This SO question covers the same land (not particularly detailed).

+8
source

You can also look at CherryPy - it is also quite simple and has several request streams without any extra effort. Although your needs may be modest now, CP has many good features that may benefit you in the future.

+1
source

Depending on your requirements, another option may be to connect a Paste . Although, based on your example, this may be redundant. Keep something in the toolbar.

0
source

Source: https://habr.com/ru/post/1304274/


All Articles