12

stdinから読み取り、PubSubメッセージを投稿しているjabberクライアントがあります。stdinでEOFを取得した場合、クライアントを終了したいと思います。

最初に試しsys.exit()ましたが、これにより例外が発生し、クライアントが終了しません。その後、検索を行ったところreactor.stop()、電話する必要があることがわかりましたが、これを機能させることができません。私のクライアントの次のコード:

from twisted.internet import reactor
reactor.stop()

結果はexceptions.AttributeError: 'module' object has no attribute 'stop'

ツイストでアプリケーションをシャットダウンして終了するにはどうすればよいですか?

編集2

元の問題は、いくつかのシンボリックリンクがモジュールのインポートを台無しにしたことが原因でした。その問題を修正した後、新しい例外が発生します。

twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.

例外の後、twistedはシャットダウンします。MyClient.loopこれは、の呼び出しが原因である可能性があると思いますMyClient.connectionInitialized。おそらく私は後でまで電話を延期する必要がありますか?

編集

これが私のクライアントの.tacファイルです

import sys

from twisted.application import service
from twisted.words.protocols.jabber.jid import JID

from myApp.clients import MyClient

clientJID = JID('client@example.com')
serverJID = JID('pubsub.example.com')
password = 'secret'

application = service.Application('XMPP client')
xmppClient = client.XMPPClient(clientJID, password)
xmppClient.logTraffic = True
xmppClient.setServiceParent(application)

handler = MyClient(clientJID, serverJID, sys.stdin)
handler.setHandlerParent(xmppClient)

私が呼び出しているのは

twistd -noy sentry/myclient.tac < input.txt

MyClientのコードは次のとおりです。

import os
import sys
import time
from datetime import datetime

from wokkel.pubsub import PubSubClient

class MyClient(PubSubClient):
    def __init__(self, entity, server, file, sender=None):
        self.entity = entity
        self.server = server
        self.sender = sender
        self.file = file

    def loop(self):
        while True:
            line = self.file.readline()
            if line:
                print line
            else:
                from twisted.internet import reactor
                reactor.stop()

    def connectionInitialized(self):
        self.loop()
4

3 に答える 3

7
from twisted.internet import reactor
reactor.stop()

それはうまくいくはずです。それがあなたのアプリケーションに何か他の問題があることを意味しないという事実。あなたが提供した情報から何が悪いのか理解できません。

より多くの(すべての)コードを提供できますか?


編集:

さて、問題はあなたがあなた自身のwhile Trueループを止めないということです、それでそれはループを続け、そして最終的には再び原子炉を止めます。

これを試して:

from twisted.internet import reactor
reactor.stop()
return

さて、あなたのループはイベント駆動型フレームワークにとってあまり良いことではないと思います。行を印刷するだけでも問題ありませんが、実際に何をしたいかによっては(行を印刷するだけではないのではないかと思います)、イベントを処理するためにそのループをリファクタリングする必要があります。

于 2011-03-17T20:48:52.300 に答える
6

reactor.callFromThread(reactor.stop)の代わりに使用してくださいreactor.stop。これで問題が解決するはずです。

于 2016-04-20T08:49:26.517 に答える
1

私は以前このように行っていました(ツイストされていないアプリケーションのsigintハンドラーで):

reactor.removeAll()
reactor.iterate()
reactor.stop()

それが正しい方法かどうかは100%わかりませんが、ツイストは幸せです

tacで開始された同じアプリケーションは、ツイストシグナルハンドラーによって直接処理されます。この質問を見つけたのは、終了する前に結果を待って処理するrpcクライアント要求があり、ツイストが許可せずにリアクターを強制終了しているように見えるためです。通話終了

于 2012-11-24T04:24:46.730 に答える