【问题标题】:Python Thread is not working simultaniouslyPython 线程不能同时工作
【发布时间】:2019-06-18 00:06:30
【问题描述】:

我目前正在使用 python,并希望通过 MQTT 接收数据,然后将其发送到服务器。当我收到“0”时,我想启动一个计时器,它应该在后台运行,这样我仍然可以获取数据并将其发送到服务器。我用一个线程启动计时器,但在我的情况下,程序会停止,直到计时器结束,然后继续接收和发送。

代码:

import threading
import time
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
     client.subscribe("test/test/projekt")

def timer_started():
     global timer_thread
     print("timer started")
     shutdown_timer = time.time()
     elapsed = 0
     while elapsed < 5:
          elapsed = time.time()-shutdown_timer
     print("Timer finished")

def on_message(client, userdata,msg):
     global thread_active 
     if msg.payload =="0" and thread_active == False:
           thread_active =True
           global timer_thread
           timer_thread.start()

timer_thread = threading.Thread(target=timer_started)
client=mqtt.CLient()
client.on_connect() = on_connect
client.on_message= on_message
client.connect("test.mosquitto.org",1883,60)
client.loop_forever()

有人知道我做错了什么吗?

【问题讨论】:

    标签: python multithreading server mqtt


    【解决方案1】:

    变量 thread_active 和 msg.payload 的比较可能是罪魁祸首。 MQTT 有效负载需要在比较之前转换为字符串。我对上面的代码进行了检查,并对其进行了修改,它可以在计时器线程中接收数据。

    以下是工作示例:

    import threading
    import time
    import paho.mqtt.client as mqtt
    
    def on_connect(client, userdata, flags, rc):
        print('connection')
        print (rc)
        client.subscribe("Test")
    
    def timer_started():
        global timer_thread, thread_active
        print("timer started")
        shutdown_timer = time.time()
        elapsed = 0
        while elapsed < 5:
            elapsed = time.time()-shutdown_timer
        print("Timer finished")
        thread_active =False
    
    def on_message(client, userdata,msg):
        print("Message")
        print(msg.payload)
        global thread_active
        if msg.payload.decode("utf-8") =="0" and thread_active == False:
            thread_active =True
            global timer_thread
            timer_thread.start()
    
    
    timer_thread = threading.Thread(target=timer_started)
    thread_active = False
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    
    client.connect("localhost",1883,60)
    client.loop_forever()
    

    在发布带有“0”的虚拟主题“Test”值时,计时器会启动,并且在计时器运行检查期间,“5”会发布到同一主题。以下是预期运行的输出:

    connection
    0
    Message
    b'0'
    timer started
    Message
    b'5'
    Timer finished
    

    【讨论】:

    • 感谢您的回复。但是在我的设备上这并没有很好地工作。如果我在计时器启动时发布一条消息,但如果我发布更多消息,则没有打印,只有在计时器完成控制台打印后,它才会收到一条消息。
    • 等一下,我再次测试了它,但略有不同。当计时器启动时,我从客户端向服务器发送消息,服务器收到它们,但没有打印到控制台。打印收到的东西是否需要做很多工作,还是有什么可疑之处?但这只有在计时器运行时在 timer_started 方法中打印某些内容时才有效……我不明白那里发生了什么……
    • 好吧,我想我明白了。我向计时器线程添加了 0.5 秒的睡眠时间,然后它就完成了我想要的操作。我现在的问题是:当我在没有中断的情况下执行一段时间方法时,是否真的没有流程更改? :D
    • @benipro 我无法理解,当您发布到一个主题时,您是否无法获得多条消息。如果在它不工作时有任何特定的用例,请使用最小的工作示例和在这种情况下获得的输出来描述它。很高兴它有帮助!
    • 当定时器启动时,我的控制台刚刚打印出“定时器启动”,之后没有打印,在定时器完成之前收到了更多消息。之后,它打印了在那段时间收到的所有消息。
    猜你喜欢
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多