我希望能够通过立即返回一条消息然后启动一个进程在后台运行并完成一项任务来响应 GET。这适用于 BaseHTTPServer.HTTPServer 和扩展 BaseHTTPRequestHandler。但是,当我按照此处的说明将 SSL 添加到服务器时,在子进程完成之前不会发送响应。假设 Firefox 在接收时显示它接收到的内容,并使用 Firebug 仔细检查,在子进程完成之前似乎什么都没有发送。我在 Chrome 中测试并看到了相同的结果。代码如下;我不知道如何继续调试。
class SecureHTTPServer(HTTPServer):
def __init__(self, server_address, HandlerClass):
BaseServer.__init__(self, server_address, HandlerClass)
ctx = SSL.Context(SSL.SSLv3_METHOD)
ctx.use_privatekey_file(SSL_CERT)
ctx.use_certificate_file(SSL_CERT)
self.socket = SSL.Connection(ctx, socket.socket(self.address_family, self.socket_type))
self.server_bind()
self.server_activate()
def shutdown_request(self, request):
request.shutdown()
class RecordingHandler(BaseHTTPRequestHandler):
def setup(self):
self.connection = self.request
self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
def do_GET(self):
key = 'abc'
p = Process(target = some_func, args = (key,))
# Some_func takes ~15 seconds to complete, and it logs its progress so that it's clear that the response is only sent after some_func is complete.
p.daemon = True
p.start()
self.send_response(200)
self.end_headers()
self.wfile.write('recording key ' + key)
def run():
try:
server = SecureHTTPServer(('', 8080), RecordingHandler)
print 'Waiting for input on port 8080.'
server.serve_forever()
except KeyboardInterrupt:
print 'Received keyboard interrupt, exiting.'
server.shutdown()