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()