Twisted:从阻塞请求返回
Twisted: returning from a blocking request
我有一个 render()
函数,它不使用延迟,因为直接 return 响应更快。我不想直接return一个bytestring,而是先写到request对象中,手动finalize然后return。重要的是我能够在第二个代码片段中使用 return 语句。
def render(self, request)
return b"not authorized"
应该变成(如果可以的话)
def render(self, request)
request.write(b"not authorized")
request.finish()
return NOT_DONE_YET
我注意到,当我 return 除了 NOT_DONE_YET
以外的任何东西时,我都会得到一个 exceptions.RuntimeError: Request.write called on a request after Request.finish was called.
有有效的 examples 使用了非常相似的序列:
request.redirect(...)
request.finish()
return NOT_DONE_YET
那么,write
/ finished
/ NOT_DONE_YET
的组合可以使用吗?
好的,根据a docstring in Twisted's source code,
render_METHOD methods are expected to return a byte string which will be
the rendered page, unless the return value is C{server.NOT_DONE_YET}, in
which case it is this class's responsibility to write the results using
C{request.write(data)} and then call C{request.finish()}.
我有一个 render()
函数,它不使用延迟,因为直接 return 响应更快。我不想直接return一个bytestring,而是先写到request对象中,手动finalize然后return。重要的是我能够在第二个代码片段中使用 return 语句。
def render(self, request)
return b"not authorized"
应该变成(如果可以的话)
def render(self, request)
request.write(b"not authorized")
request.finish()
return NOT_DONE_YET
我注意到,当我 return 除了 NOT_DONE_YET
以外的任何东西时,我都会得到一个 exceptions.RuntimeError: Request.write called on a request after Request.finish was called.
有有效的 examples 使用了非常相似的序列:
request.redirect(...)
request.finish()
return NOT_DONE_YET
那么,write
/ finished
/ NOT_DONE_YET
的组合可以使用吗?
好的,根据a docstring in Twisted's source code,
render_METHOD methods are expected to return a byte string which will be the rendered page, unless the return value is C{server.NOT_DONE_YET}, in which case it is this class's responsibility to write the results using C{request.write(data)} and then call C{request.finish()}.