我有python3代码,它使用ThreadingMixIn实现HttpServer。 问题是我有一个复杂的张量流计算,它在服务器启动时运行。
当longComputation = Long_Computation()
在里面时(线程化)RequestHandler
它可以工作但是每次重建请求都太费时了
当它出局时,如下面的代码,我得到了
longComputation = Long_Computation()
class RequestHandler(BaseHTTPRequestHandler):
# if i add longComputation = Long_Computation() here, it works but reruns code for each request
self.send_response(200)
self.end_headers()
self.send_header('Content-type','application/json')
length = int(self.headers['content-length'])
data = self.rfile.read(length).decode('utf-8')
longComputation.run({'data':data})
self.wfile.write(bytes(json.dumps({}), "utf8"))
return
return
class ThreadedHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
def run():
print('starting server...')
server_address = ('0.0.0.0', PORT)
httpd = ThreadedHTTPServer(server_address, RequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile='/etc/letsencrypt/live/classifier-icici.nanorep-services.com/privkey.pem', certfile='/etc/letsencrypt/live/classifier-icici.nanorep-services.com/cert.pem', server_side=True)
httpd.serve_forever()