在KeyboardInterrupt之后,最后块没有执行。

时间:2018-06-16 19:33:35

标签: python python-3.x

我使用多个MQTT客户端作为线程,一些作为订阅者而另一些作为发布者。发布者线程工作正常,但订阅者线程不是。

def subscribe_topic(self, clientID, topic):
    self.initiate_connection_to_broker(clientID)
    try:            
        self.mqtt_client.subscribe(topic, qos = 0)
        self.mqtt_client.loop_forever()         
    except KeyboardInterrupt:
        print("Disconnecting...!!!!")
    finally:
        self.disconnect_from_broker()

现在因为" loop_forever()",它必须永远运行。我的问题是当我试图通过" KeyboardInterrupt(ctrl + c)"而不是运行"而不是最后阻止"来阻止用户线程。我到了下面:

^CException ignored in: <module 'threading' from '/usr/lib/python3.5/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 1288, in _shutdown
    t.join()
  File "/usr/lib/python3.5/threading.py", line 1054, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

然后退出。 我打电话给#34; subscribe_topic&#34;来自&#34; run&#34;继承threading.Thread类的方法。方法&#34; subscribe_topic&#34;在另一个类中定义。 我想执行&#34;除了最后阻止&#34;键盘中断后。谁能告诉我解决方案或我需要做出哪些更正?

0 个答案:

没有答案