我有一个用 Twisted Web 编写的前端 Web 服务器,它与另一个 Web 服务器连接。客户端将文件上传到我的前端服务器,然后将文件发送到后端服务器。我想接收上传的文件,然后在将文件发送到后端服务器之前立即向客户端发送响应。这样,客户端不必等待两个上传都发生后才能得到响应。
我试图通过在单独的线程中开始上传到后端服务器来做到这一点。问题是,在向客户端发送响应后,我不再能够从Request
对象访问上传的文件。这是一个例子:
class PubDir(Resource):
def render_POST(self, request):
if request.args["t"][0] == 'upload':
thread.start_new_thread(self.upload, (request,))
### Send response to client while the file gets uploaded to the back-end server:
return redirectTo('http://example.com/uploadpage')
def upload(self, request):
postheaders = request.getAllHeaders()
try:
postfile = cgi.FieldStorage(
fp = request.content,
headers = postheaders,
environ = {'REQUEST_METHOD':'POST',
'CONTENT_TYPE': postheaders['content-type'],
}
)
except Exception as e:
print 'something went wrong: ' + str(e)
filename = postfile["file"].filename
file = request.args["file"][0]
#code to upload file to back-end server goes here...
当我尝试这个时,我得到一个错误:I/O operation on closed file
。