网站wap转换,asp.net制作网站开发,seo服务标准,html怎么设置网站吗我正在尝试编写一个简单的http服务器来处理在数据结构中查找响应或超时的异步请求#xff1a;请求到达时间如果回复,请将其退回如果超时,则返回超时消息我是新手,我想知道做异步响应的最佳方法是什么.我看了some twisted Deferred docs和callLater,但我不清楚…我正在尝试编写一个简单的http服务器来处理在数据结构中查找响应或超时的异步请求请求到达时间如果回复,请将其退回如果超时,则返回超时消息我是新手,我想知道做异步响应的最佳方法是什么.我看了some twisted Deferred docs和callLater,但我不清楚到底应该做些什么.现在我使用deferToThread运行阻塞方法并等待超时.我的延迟方法得到一个字符串不可调用的错误Unhandled error in Deferred:Traceback (most recent call last):File /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py, line 497, in __bootstrapself.__bootstrap_inner()File /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py, line 522, in __bootstrap_innerself.run()File /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py, line 477, in runself.__target(*self.__args, **self.__kwargs)--- ---File /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/twisted/python/threadpool.py, line 210, in _workerresult context.call(ctx, function, *args, **kwargs)File /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/twisted/python/context.py, line 59, in callWithContextreturn self.currentContext().callWithContext(ctx, func, *args, **kw)File /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/twisted/python/context.py, line 37, in callWithContextreturn func(*args,**kw)exceptions.TypeError: str object is not callable这是我的代码from twisted.web import server, resourcefrom twisted.internet import reactor, threadsimport jsonimport timeconnectedClients {}responseCollector {}# add fake data to the collectorclass FakeData(resource.Resource):isLeaf Truedef render_GET(self, request):request.setHeader(content-type, application/json)if rid in request.args and data in request.args:rid request.args[rid][0]data request.args[data][0]responseCollector[str(rid)] datareturn json.dumps(responseCollector)return {}class RequestHandler(resource.Resource):isLeaf Truedef render_GET(self, request):#request.setHeader(content-type, application/json)if not (rid in request.args and and json in request.args):return {success:false,response:invalid request}rid request.args[rid][0]json request.args[id][0]# TODO: Wait for data to show up in the responseCollector with same rid# as our request without blocking other requests OR timeoutd threads.deferToThread(self.blockingMethod(rid))d.addCallback(self.ret)d.addErrback(self.err)def blockingMethod(self,rid):timeout 5.0timeElapsed 0.0while timeElapsedif rid in responseCollector:return responseCollector[rid]else:timeElapsed0.01time.sleep(0.01)return timeoutdef ret(self, hdata):return hdatadef err(self, failure):return failurereactor.listenTCP(8080, server.Site(RequestHandler()))reactor.listenTCP(9080, server.Site(FakeData()))reactor.run()发出请求(当前没有返回任何有用的内容)http://localhost:8080/?rid1234json{%22foo%22:%22bar%22}添加一些假数据以用于请求http://localhost:9080/?rid1234datafoo更新了工作版本from twisted.web import server, resourcefrom twisted.internet import reactor, threadsimport jsonimport timeconnectedClients {}responseCollector {}# add fake data to the collectorclass FakeData(resource.Resource):isLeaf Truedef render_GET(self, request):request.setHeader(content-type, application/json)if rid in request.args and data in request.args:rid request.args[rid][0]data request.args[data][0]responseCollector[str(rid)] datareturn json.dumps(responseCollector)return {}class RequestHandler(resource.Resource):isLeaf Truedef render_GET(self, request):if not (rid in request.args and data in request.args):return {success:false,response:invalid request}rid request.args[rid][0]json request.args[data][0]# TODO: Wait for data to show up in the responseCollector with same rid# as our request without blocking other requests OR timeoutd threads.deferToThread(self.blockingMethod,rid)d.addCallback(self.ret, request)d.addErrback(self.err)return server.NOT_DONE_YETdef blockingMethod(self,rid):timeout 5.0timeElapsed 0.0while timeElapsedif rid in responseCollector:return responseCollector[rid]else:timeElapsed0.01time.sleep(0.01)return timeoutdef ret(self, result, request):request.write(result)request.finish()def err(self, failure):return failurereactor.listenTCP(8080, server.Site(RequestHandler()))reactor.listenTCP(9080, server.Site(FakeData()))reactor.run()解决方法:在render_GET()中,您应该返回twisted.web.server.NOT_DONE_YET.您应该将请求对象传递给ret方法d.addCallback(self.ret,request)然后在ret(请求)中,您应该使用request.write(hdata)编写异步数据并关闭与request.finish()的连接.def ret(self, result, request):request.write(result)request.finish()Resource rendering occurs when Twisted Web locates a leaf Resourceobject to handle a web request. A Resource’s render method may dovarious things to produce output which will be sent back to thebrowser:Return a stringRequest a Deferred, return server.NOT_DONE_YET,and call request.write(“stuff”) and request.finish() later, in acallback on the Deferred.标签python,twisted,twisted-web来源 https://codeday.me/bug/20190729/1573193.html