3

我正在尝试编写一个简单的 http 服务器来处理在数据结构中查找响应或超时的异步请求:

  1. 请求到达
  2. 当时间 < 超时检查 responseCollector 的响应(使用 requestId 作为键)
  3. 如果响应,返回它
  4. 如果超时,则返回超时消息

我是扭曲的新手,想知道做异步响应的最佳方法是什么。我查看了一些扭曲的 Deferred 文档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)
--- <exception caught here> ---
  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<timeout:
            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<timeout:
            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()
4

2 回答 2

4

在 render_GET() 中,您应该返回 twisted.web.server.NOT_DONE_YET。您应该将请求对象传递给 ret 方法: d.addCallback(self.ret, request)

然后在 ret(request) 中,您应该使用 request.write(hdata) 写入异步数据并使用 request.finish() 关闭连接。

def ret(self, result, request):
    request.write(result)
    request.finish()

一些信息来自:http ://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html

当 Twisted Web 找到一个叶资源对象来处理 Web 请求时,就会发生资源渲染。Resource 的 render 方法可以做各种事情来产生将被发送回浏览器的输出:

  • 返回一个字符串
  • 请求一个 Deferred,返回 server.NOT_DONE_YET,稍后在 Deferred 的回调中调用 request.write("stuff") 和 request.finish()。
于 2012-05-14T01:55:26.860 回答
3

想想您的示例中这两个版本的代码行之间的区别:

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

对比

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

阅读API 文档deferToThread或者阅读一些关于函数对象的 Python 文档(python.org 文档没有很好地涵盖这一点,但很多第三方文档可以)。

于 2012-05-14T12:40:20.903 回答