首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Twisted阻止Twisted调用

用Twisted阻止Twisted调用
EN

Stack Overflow用户
提问于 2011-10-19 03:22:57
回答 2查看 1.6K关注 0票数 1

我有一个使用TTwisted协议的Twisted/Thrift服务器。我希望保持从客户端的连接打开,直到一个给定的事件发生(通过回调Twisted反应堆通知我这个事件)。

代码语言:javascript
复制
class ServiceHandler(object):
    interface.implements(Service.Iface)

    def listenForEvent(self):
        """
        A method defined in my Thrift interface that should block the Thrift
        response until my event of interest occurs.
        """
        # I need to somehow return immediately to free up the reactor thread,
        # but I don't want the Thrift call to be considered completed. The current
        # request needs to be marked as "waiting" somehow.

    def handleEvent(self):
        """
        A method called when the event of interest occurs. It is a callback method
        registered with the Twisted reactor.
        """
        # Find all the "waiting" requests and notify them that the event occurred.
        # This will cause all of the Thrift requests to complete.

如何快速地从我的处理程序对象的方法返回,同时保持阻塞的线程调用的错觉?

我从Twisted启动触发器初始化了trigger处理程序:

代码语言:javascript
复制
def on_startup():
    handler = ServiceHandler()                      
    processor = Service.Processor(handler)                                   
    server = TTwisted.ThriftServerFactory(processor=processor,                  
        iprot_factory=TBinaryProtocol.TBinaryProtocolFactory())                 
    reactor.listenTCP(9160, server)

我的PHP客户端连接:

代码语言:javascript
复制
  $socket = new TSocket('localhost', 9160);
  $transport = new TFramedTransport($socket);
  $protocol = new TBinaryProtocol($transport);
  $client = new ServiceClient($protocol);
  $transport->open();
  $client->listenForEvent();

最后一次调用($client->listenForEvent())成功地转移到服务器并运行ServiceHandler.listenForEvent,但即使该服务器方法返回twisted.internet.defer.Deferred()实例,客户端立即收到一个空数组,我得到了异常:

异常'TTransportException‘与消息'TSocket:超时从本地主机9160读取4个字节到本地端口38395'

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-19 13:00:43

您应该能够从listenForEvent返回一个延迟推断。稍后,handleEvent应该触发返回延迟推断(或那些返回的Deferreds)的对象来实际生成响应。

票数 2
EN

Stack Overflow用户

发布于 2011-10-20 13:33:17

您所看到的错误似乎表明传输没有帧(Twisted需要它来预先知道每条消息的长度)。此外,Th深层服务器支持从处理程序返回延迟,因此这更奇怪。您是否尝试过返回defer.succeed(“一些值”)并查看延迟是否有效?然后,您可以转到这里来检查它是否完全工作:

代码语言:javascript
复制
    d = defer.Deferred()
    reactor.callLater(0, d.callback, results)
    return d
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7816202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档