我有一个使用TTwisted协议的Twisted/Thrift服务器。我希望保持从客户端的连接打开,直到一个给定的事件发生(通过回调Twisted反应堆通知我这个事件)。
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处理程序:
def on_startup():
handler = ServiceHandler()
processor = Service.Processor(handler)
server = TTwisted.ThriftServerFactory(processor=processor,
iprot_factory=TBinaryProtocol.TBinaryProtocolFactory())
reactor.listenTCP(9160, server)我的PHP客户端连接:
$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'
发布于 2011-10-19 13:00:43
您应该能够从listenForEvent返回一个延迟推断。稍后,handleEvent应该触发返回延迟推断(或那些返回的Deferreds)的对象来实际生成响应。
发布于 2011-10-20 13:33:17
您所看到的错误似乎表明传输没有帧(Twisted需要它来预先知道每条消息的长度)。此外,Th深层服务器支持从处理程序返回延迟,因此这更奇怪。您是否尝试过返回defer.succeed(“一些值”)并查看延迟是否有效?然后,您可以转到这里来检查它是否完全工作:
d = defer.Deferred()
reactor.callLater(0, d.callback, results)
return dhttps://stackoverflow.com/questions/7816202
复制相似问题