[英]Blocking Thrift calls with Twisted
我有一個使用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.
如何在保持阻塞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()
實例,客戶端也將立即收到一個空數組,我得到異常:
異常'TTransportException',消息'TSocket:timed out read 4 bytes from localhost:9160 to local port 38395'
您應該能夠從listenForEvent
返回Deferred。 稍后handleEvent
應該觸發返回的Deferred(或那些返回的Deferreds)來實際生成響應。
您看到的錯誤似乎表明傳輸沒有框架(Twisted需要這樣,以便它可以事先知道每條消息的長度)。 此外,Thrift服務器支持從處理程序返回延遲,因此更奇怪。 您是否嘗試過返回defer.succeed(“某些值”)並查看延遲實際是否有效? 然后,您可以轉到此位置以檢查它是否可以正常運行:
d = defer.Deferred()
reactor.callLater(0, d.callback, results)
return d
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.