Reputation: 1560
I have a small demo page served with http.server. I tried sharing with my coworkers but discovered that http.server remains blocked on any open connection, so concurrent users can't be served. Is there a way to run http.server to handle concurrent connections? I found nothing useful here: https://docs.python.org/3/library/http.server.html
Upvotes: 4
Views: 4653
Reputation: 31
ThreadingHTTPServer does not handle concurrent requests from multiple browsers, but there is a workaround to do it by preventing HTTPServer from re-binding its socket every instance (answer by personal_cloud): streaming HTTP server supporting multiple connections on one port (You must fixup the imports, and append .encode() to every string argument to write().)
Using that approach my Raspberry Pi 3B+ can stream its camera as a series of still JPEG images to 8 simultaneous browsers at 30 frames per second (same as it gets for 1 user). This is essentially MJPEG, and is much lower latency (<< 1 second) than any video encoding. The camera uses ~70% of a core, and each stream adds ~2%; 8 streams use ~2.5 MB/sec on the network.
Upvotes: 0
Reputation: 14964
You can now use ThreadingHTTPServer
class http.server.ThreadingHTTPServer(server_address, RequestHandlerClass)
This class is identical to HTTPServer but uses threads to handle requests by using the ThreadingMixIn. This is useful to handle web browsers pre-opening sockets, on which HTTPServer would wait indefinitely.
New in version 3.7.
Upvotes: 2
Reputation: 16624
IIRC there is no existing config option, but you could extend one with socketserver.ThreadingMixin
if you like:
import sys
import socketserver
import http.server
class ThreadedHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
daemon_threads = True
port = int(sys.argv[1])
server = ThreadedHTTPServer(('', port), http.server.SimpleHTTPRequestHandler)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
ps: there is a related python ticket.
Upvotes: 6