2011-10-19 4 views
1

TTwistedプロトコルを使用するTwisted/Thriftサーバーがあります。私は、クライアントからの接続を、所定のイベントが発生するまで開いておきたいと思う(私はこのイベントをコールバックを介してTwisted reactorによって通知する)。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. 

ブロッキング・スリフト・コールの錯覚を維持しながらハンドラ・オブジェクトのメソッドから迅速に戻ることができますか?


私はツイスト起動トリガからスリフトハンドラの初期化:最後の呼び出し($client->listenForEvent())が正常にそれを上に作ること

$socket = new TSocket('localhost', 9160); 
    $transport = new TFramedTransport($socket); 
    $protocol = new TBinaryProtocol($transport); 
    $client = new ServiceClient($protocol); 
    $transport->open(); 
    $client->listenForEvent(); 

:PHPでの私のクライアントはと接続

def on_startup(): 
    handler = ServiceHandler()      
    processor = Service.Processor(handler)         
    server = TTwisted.ThriftServerFactory(processor=processor,     
     iprot_factory=TBinaryProtocol.TBinaryProtocolFactory())     
    reactor.listenTCP(9160, server) 

をサーバーはServiceHandler.listenForEventを実行しますが、そのサーバーメソッドがtwisted.internet.defer.Deferred()インスタンスを返す場合でも、クライアントはすぐに空の配列を受け取り、I geトン例外:メッセージと

例外「TTransportException」「TSocketは: は、ローカルホストからの4バイトの読み込みタイムアウトになりました:ローカルポート38395へ9160」を

答えて

2

あなたはlistenForEventから繰延返すことができるはずです。後でhandleEventは返されたDeferred(またはDeferredsが返されたもの)を起動して実際に応答を生成する必要があります。

+0

これを試しました。私の問題は、クライアントからのTTwistedトランスポートも使用する必要がありますか?私はPHPからTFramedTransportを使用しています。 – ide

+0

遅延したものを返す。問題は、クライアントソケットのタイムアウトがわずか750ミリ秒であったことです。 – ide

1

表示されているエラーは、トランスポートがフレーム化されていないことを示しているようです(Twistedは、各メッセージの長さを事前に知る必要がある)。また、Thriftサーバーは、ハンドラーからの遅延を返すことをサポートしているので、さらに奇妙です。 defer.succeed( "some value")を返そうとしましたが、実際にdeferredsが機能するかどうかを確認しましたか?次に、これに移動して完全に機能することを確認します。

d = defer.Deferred() 
    reactor.callLater(0, d.callback, results) 
    return d 
関連する問題