multithreading - proper threading in python -



multithreading - proper threading in python -

i writing home automation helpers - little daemon-like python applications. can run each separate process since there made decided set little dispatcher spawn each of daemons in own threads , able deed shall thread die in future.

this looks (working 2 classes):

from daemons import mosquitto_daemon, gtalk_daemon 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() , gt.isalive(): pass print('something died')

the problem mq daemon (moquitto) work fine shall run directly:

mq_client = mosquitto_daemon.client() mq_client.run()

it start , hang in there listening messages nail relevant topics - i'm looking for.

however, run within dispatcher makes deed weirdly - receive single message , stop acting yet thread reported alive. given works fine without threading woodoo i'm assuming i'm doing wrong in dispatcher.

i'm quoting mq client code in case:

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 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 topics...') topic in config.mq_topics: result, some_number = client.mc.subscribe(topic, 0) if result == 0: logging.debug('subscription topic "%s" successful' % topic) else: logging.error('failed subscribe topic "%s": %s' % (topic, result)) logging.debug('settings callbacks...') self.mc.on_message = self.on_message logging.info('finished initialization') except exception e: logging.critical('failed finish 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))

you passing thread class instance's run method... doesn't know it.

threading.thread can used in 2 general ways: spawn thread wrapped independent function, or base of operations class class run method. in case appears baseclass way go, since client class has run method.

replace next in mq class , should work:

from threading import thread class client(thread): mc = none def __init__(self): thread.__init__(self) # initialize thread instance ... ... def stop(self): # sort of command stop mc self.mc.stop() # not sure actual command is, if 1 exists @ all...

then when calling it, without thread:

mq_client = mosquitto_daemon.client() mq_client.start() print 'print line sure here after starting thread loop...'

python multithreading mq

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -