私はホーム オートメーション ヘルパーを作成しています。これらは基本的にデーモンのような小さな Python アプリケーションです。それらはそれぞれ個別のプロセスとして実行できますが、作成されるので、それぞれのスレッドで各デーモンを生成し、将来スレッドが死んでも動作できるようにする小さなディスパッチャを作成することにしました。
これは次のようになります (2 つのクラスで作業):
from daemons import mosquitto_daemon, gtalk_daemon
from threading import Thread
print('Starting daemons')
mq_client = mosquitto_daemon.Client()
gt_client = gtalk_daemon.Client()
print('Starting MQ')
mq = Thread(target=mq_client.run)
mq.start()
print('Starting GT')
gt = Thread(target=gt_client.run)
gt.start()
while mq.isAlive() and gt.isAlive():
pass
print('something died')
問題は、MQ デーモン (moquitto) が正常に動作し、直接実行する必要があることです。
mq_client = mosquitto_daemon.Client()
mq_client.run()
関連するトピックにヒットするすべてのメッセージを聞いて、そこにハングアップします-まさに私が探しているものです。
ただし、ディスパッチャー内で実行すると、奇妙な動作になります。単一のメッセージを受信した後、動作を停止しますが、スレッドは生きていると報告されます。スレッド Woodoo がなくても問題なく動作することを考えると、私はディスパッチャーで何か間違ったことをしていると思います。
念のため、MQ クライアント コードを引用します。
import mosquitto
import config
import sys
import logging
class Client():
mc = None
def __init__(self):
logging.basicConfig(format=u'%(filename)s:%(lineno)d %(levelname)-8s [%(asctime)s] %(message)s', level=logging.DEBUG)
logging.debug('Class initialization...')
if not Client.mc:
logging.info('Creating an instance of MQ client...')
try:
Client.mc = mosquitto.Mosquitto(config.DEVICE_NAME)
Client.mc.connect(host=config.MQ_BROKER_ADDRESS)
logging.debug('Successfully created MQ client...')
logging.debug('Subscribing to topics...')
for topic in config.MQ_TOPICS:
result, some_number = Client.mc.subscribe(topic, 0)
if result == 0:
logging.debug('Subscription to topic "%s" successful' % topic)
else:
logging.error('Failed to subscribe to topic "%s": %s' % (topic, result))
logging.debug('Settings up callbacks...')
self.mc.on_message = self.on_message
logging.info('Finished initialization')
except Exception as e:
logging.critical('Failed to complete creating MQ client: %s' % e.message)
self.mc = None
else:
logging.critical('Instance of MQ Client exists - passing...')
sys.exit(status=1)
def run(self):
self.mc.loop_forever()
def on_message(self, mosq, obj, msg):
print('meesage!!111')
logging.info('Message received on topic %s: %s' % (msg.topic, msg.payload))