Python ThreadingHTTPServer未实现多线程的问题及解决方法
解决ThreadingHTTPServer未真正实现多线程的问题
问题原因
ThreadingHTTPServer本身是支持多线程处理请求的,你遇到串行执行的情况,大概率是测试方式导致的:比如使用同一浏览器窗口的标签页发送请求时,浏览器会对同一域名的请求做串行化限制(避免同时发起过多请求),导致你误以为服务器是串行处理的。
验证方式
用命令行工具(如curl)并发发送请求,或者用Python脚本发起异步请求,才能准确测试服务器的多线程能力:
import requests from concurrent.futures import ThreadPoolExecutor def send_request(): resp = requests.get("http://localhost:8080") print(resp.text) if __name__ == "__main__": with ThreadPoolExecutor(max_workers=2) as executor: executor.submit(send_request) executor.submit(send_request)
运行这个脚本,你会看到两个请求的起始时间几乎一致,说明服务器是并行处理的。
进阶优化(可选)
如果需要更灵活的线程管理,可以自定义服务器类,使用concurrent.futures.ThreadPoolExecutor替代默认的线程创建方式,能更好地控制并发数,避免频繁创建销毁线程的开销:
from http.server import HTTPServer, BaseHTTPRequestHandler from concurrent.futures import ThreadPoolExecutor import datetime import random import time class Handler(BaseHTTPRequestHandler): def do_GET(self): start = datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S") wait_time = random.randint(2, 6) time.sleep(wait_time) response = f"{start}: wait {wait_time}" self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(response.encode('utf-8')) class PooledHTTPServer(HTTPServer): def __init__(self, server_address, RequestHandlerClass): super().__init__(server_address, RequestHandlerClass) self.executor = ThreadPoolExecutor(max_workers=4) # 自定义线程池大小 def process_request(self, request, client_address): self.executor.submit(self._process_request_thread, request, client_address) def _process_request_thread(self, request, client_address): try: self.finish_request(request, client_address) self.close_request(request) except Exception: self.handle_error(request, client_address) self.close_request(request) def start_http_server(): httpd = PooledHTTPServer(("", 8080), Handler) httpd.serve_forever() if __name__ == "__main__": start_http_server()
内容的提问来源于stack exchange,提问作者Serg
相关产品推荐
相关产品推荐

