我有一个使用 python 3.8 http.server 创建的简单 HTTP 服务器。
2 个端点 1:/restart 在后台执行线程 2:/check_status
当我请求第一个端点时,它执行得很好,第二个请求是收到套接字连接被拒绝错误,直到线程执行完成。请让我知道是否有更好的方法来做到这一点。
def restart_solr_service():
tr = SolrRestartThread()
class SolrRestartThread(object):
def __init__(self, interval=1):
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()
def run(self):
logger.info(execute_cmd(solr_restart_cmd,status_workspace,logger))
logger.info("Restart solr completed")
wait_for_solr_service()
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
#self.send_response(200)
response_code= 200
request_key = self.path.replace('/', '')
if request_key in get_op_dict:
print(request_key)
body_content,response_code = get_op_dict[request_key]()
print(response_code)
else:
body_content = "invalid request.\n<br>Hostname:" + socket.gethostname()
self.send_response(response_code)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><body>", "utf-8"))
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("<p>" + body_content + " </p> ", "utf-8"))
self.wfile.write(bytes("<p>Response code:" + str(response_code) + " </p> ", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))