Reputation: 49
I am trying to run some test in parallel upon on_message
callback. When my on_message
callback gets called, based on some message, I create a thread to run some test and set it as daemon thread. When I run, I get this run-time error.
"cannot set daemon status of active thread".
#this is the callback attached to paho.mqtt.client client.on_message = self.on_message
def on_message(self, client, userdata, msg):
if msg.topic.startswith("hello/"):
#set some vlaues
elif msg.topic.startswith("data/"):
#set some vlaues
elif msg.topic.startswith("test/"):
value = msg.payload.decode("utf-8")
lst = value.split('-')
test = Test()
test._value = lstr[0]
t = Thread(target = test.callmethod,args=(lst[1]))
t.start()
t.setDaemon(True)
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: mymethod() takes 2 positional arguments but 10 were given
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 2650, in _thread_main
self.loop_forever(retry_first_connection=True)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1481, in loop_forever
rc = self.loop(timeout, max_packets)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1003, in loop
rc = self.loop_read(max_packets)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1284, in loop_read
rc = self._packet_read()
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1849, in _packet_read
rc = self._packet_handle()
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 2305, in _packet_handle
return self._handle_publish()
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 2500, in _handle_publish
self._handle_on_message(message)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 2647, in _handle_on_message
self.on_message(self, self._userdata, message)
File "/usr/local/lib/python3.6/site-packages/myproject-0.0.1-py3.6.egg/subscribe/client.py", line 60, in on_message
t.setDaemon(True)
File "/usr/local/lib/python3.6/threading.py", line 1148, in setDaemon
self.daemon = daemonic
File "/usr/local/lib/python3.6/threading.py", line 1141, in daemon
raise RuntimeError("cannot set daemon status of active thread")
RuntimeError: cannot set daemon status of active thread
Upvotes: 2
Views: 6853
Reputation: 1994
setDaemon
should be used before start
:
t = Thread(target = test.callmethod,args=(lst[1]))
t.setDaemon(True) # first
t.start() # second
Upvotes: 4