日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

http服务器异步响应,python – 具有异步响应的Twisted http服务器,其中请求必须等待数据变为可用或超时...

發布時間:2023/12/4 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 http服务器异步响应,python – 具有异步响应的Twisted http服务器,其中请求必须等待数据变为可用或超时... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在嘗試編寫一個簡單的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 __bootstrap

self.__bootstrap_inner()

File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner

self.run()

File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 477, in run

self.__target(*self.__args, **self.__kwargs)

--- ---

File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/twisted/python/threadpool.py", line 210, in _worker

result = 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 callWithContext

return 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 callWithContext

return func(*args,**kw)

exceptions.TypeError: 'str' object is not callable

這是我的代碼:

from twisted.web import server, resource

from twisted.internet import reactor, threads

import json

import time

connectedClients = {}

responseCollector = {}

# add fake data to the collector

class FakeData(resource.Resource):

isLeaf = True

def 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)] = data

return json.dumps(responseCollector)

return "{}"

class RequestHandler(resource.Resource):

isLeaf = True

def 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 timeout

d = threads.deferToThread(self.blockingMethod(rid))

d.addCallback(self.ret)

d.addErrback(self.err)

def blockingMethod(self,rid):

timeout = 5.0

timeElapsed = 0.0

while timeElapsed

if rid in responseCollector:

return responseCollector[rid]

else:

timeElapsed+=0.01

time.sleep(0.01)

return "timeout"

def ret(self, hdata):

return hdata

def err(self, failure):

return failure

reactor.listenTCP(8080, server.Site(RequestHandler()))

reactor.listenTCP(9080, server.Site(FakeData()))

reactor.run()

發出請求(當前沒有返回任何有用的內容):

http://localhost:8080/?rid=1234&json={%22foo%22:%22bar%22}

添加一些假數據以用于請求:

http://localhost:9080/?rid=1234&data=foo

更新了工作版本

from twisted.web import server, resource

from twisted.internet import reactor, threads

import json

import time

connectedClients = {}

responseCollector = {}

# add fake data to the collector

class FakeData(resource.Resource):

isLeaf = True

def 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)] = data

return json.dumps(responseCollector)

return "{}"

class RequestHandler(resource.Resource):

isLeaf = True

def 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 timeout

d = threads.deferToThread(self.blockingMethod,rid)

d.addCallback(self.ret, request)

d.addErrback(self.err)

return server.NOT_DONE_YET

def blockingMethod(self,rid):

timeout = 5.0

timeElapsed = 0.0

while timeElapsed

if rid in responseCollector:

return responseCollector[rid]

else:

timeElapsed+=0.01

time.sleep(0.01)

return "timeout"

def ret(self, result, request):

request.write(result)

request.finish()

def err(self, failure):

return failure

reactor.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 Resource

object to handle a web request. A Resource’s render method may do

various things to produce output which will be sent back to the

browser:

Return a string

Request a Deferred, return server.NOT_DONE_YET,

and call request.write(“stuff”) and request.finish() later, in a

callback on the Deferred.

標簽:python,twisted,twisted-web

來源: https://codeday.me/bug/20190729/1573193.html

總結

以上是生活随笔為你收集整理的http服务器异步响应,python – 具有异步响应的Twisted http服务器,其中请求必须等待数据变为可用或超时...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。