Python 3.x Python paho mqtt阻塞客户机循环

Python 3.x Python paho mqtt阻塞客户机循环,python-3.x,mqtt,python-multithreading,paho,Python 3.x,Mqtt,Python Multithreading,Paho,目前,我正在尝试编写一个模拟攻击的程序,该程序生成以下代码: import requests import threading import paho.mqtt.client as mqtt class Attack(object): def __init__(self): self.client = mqtt.Client() self.client.on_connect = self.on_connect self.client.

目前,我正在尝试编写一个模拟攻击的程序,该程序生成以下代码:

import requests
import threading
import paho.mqtt.client as mqtt

class Attack(object):

    def __init__(self):

        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_message = self.on_message

        self.client.connect("test.mosquitto.org")
        self.client.loop_forever()

    def poll_heise(self):
        while(True):
            time.sleep(2)
            r = requests.get('https://heise.de')

    def on_connect(self):
        self.client.subscribe("ATTACK")
        thread = threading.Thread(target=self.poll_heise)
        thread.start()

    def on_message(self):
        for i in range(1,80):
            thread = threading.Thread(target=self.write_file,args=(i,))
            thread.start()

    def write_file(self,suffix):
        new_file = open("file{0}".format(suffix),"w")
        new_file.write("testtesttesttest")
        new_file.close()


if __name__ == "__main__":
    attack = Attack()
基本上,我想做的是生成一个保持的独特行为(例如,使用请求轮询heise.de),然后在主题“攻击”的MQTT消息到达时中断此行为

然而,当我启动代码并试图通过在“攻击”上发布到test.mosquito.org来触发on_消息方法时,我什么也得不到。据我所知,解释器甚至无法访问on_消息回调。我尝试手动发布和订阅mqtt代理,结果成功了

有人知道为什么这样不行吗

_ 编辑:
我怀疑这是我处理线程的方式或某个循环阻塞的问题,但我无法确定是哪一个。非常感谢。

self.client.loop\u forever()
是一个阻塞调用,因此您的
\u init\u
函数将永远不会返回

查看
client.start\u loop()
函数