我是Twisted的新手,我无法理解为什么在运行脚本时出现此错误。\
基本上,脚本由2个页面组成,第一个是调用自身的HTML表单执行阻塞方法并显示结果。当请求同时发送到服务器时,我收到此错误:
exceptions.RuntimeError:在调用Request.finish后调用请求时调用了Request.write
你知道它可以来自哪里吗?
class FormPage(Resource):
def render_GET(self, request):
return
<html>
<body>
<form method="POST">
<input name="the-field" type="text" />
<input type='submit' name='Get udl' />
</form>
</body>
</html>
def render_POST(self, request):
deferred= deferToThread(my_blocking_method, arg1, arg2)
deferred.addCallback(self.print_success, request)
deferred.addCallback(self.print_failure, request)
return NOT_DONE_YET
def print_success(self, result, request):
request.write('Request executed successfully')
request.finish()
def print_failure(self, err, request):
request.write(str(err))
request.finish()
def run()
root = Resource()
root.putChild("form", FormPage())
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()
答案 0 :(得分:1)
我终于找到了解决方案。代码必须修改如下。 需要将第二个addCallback的行更改为addErrback。
class FormPage(Resource):
def render_GET(self, request):
return
<html>
<body>
<form method="POST">
<input name="the-field" type="text" />
<input type='submit' name='Get udl' />
</form>
</body>
</html>
def render_POST(self, request):
deferred= deferToThread(my_blocking_method, arg1, arg2)
deferred.addCallback(self.print_success, request)
deferred.addErrback(self.print_failure, request)
return NOT_DONE_YET
def print_success(self, result, request):
request.write('Request executed successfully')
request.finish()
def print_failure(self, err, request):
request.write(str(err))
request.finish()
def run()
root = Resource()
root.putChild("form", FormPage())
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()