我试图在on_message
回调时并行运行一些测试。当我的on_message
回调被调用时,根据一些消息,我创建一个线程来运行一些测试并将其设置为守护线程。当我运行时,我得到这个运行时错误。
"无法设置活动线程的守护程序状态"。
#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
答案 0 :(得分:1)
setDaemon
:之前应该使用 start
t = Thread(target = test.callmethod,args=(lst[1]))
t.setDaemon(True) # first
t.start() # second