gpt4 book ai didi

Python 扭曲错误 : "Request.write called on a request after Request.finish was called"

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:42 27 4
gpt4 key购买 nike

我是 Twisted 的新手,我不明白为什么在运行我的脚本时会出现此错误。\

基本上,该脚本由 2 个页面组成,第一个页面是一个 HTML 表单,它调用自身执行一个阻塞方法并显示结果。当请求同时发送到服务器时,我收到此错误:

exceptions.RuntimeError: Request.write 在调用 Request.finish 之后调用请求

你知道它来自哪里吗?

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()

最佳答案

我终于找到了解决方案。代码必须修改如下。\第二个 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()

关于Python 扭曲错误 : "Request.write called on a request after Request.finish was called",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8384648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com