gpt4 book ai didi

python - 使用 Twisted 阻止 Thrift 调用

转载 作者:行者123 更新时间:2023-11-30 23:49:03 26 4
gpt4 key购买 nike

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

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.

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

<小时/>

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

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() 实例,客户端立即收到一个空数组,并且出现异常:

exception 'TTransportException' with message 'TSocket: timed out reading 4 bytes from localhost:9160 to local port 38395'

最佳答案

您应该能够从 listenForEvent 返回一个 Deferred。稍后 handleEvent 应该触发返回的 Deferred(或返回的 Deferreds)以实际生成响应。

关于python - 使用 Twisted 阻止 Thrift 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7816202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com