【问题标题】:How to end properly 2 looping threading? [duplicate]如何正确结束 2 循环线程? [复制]
【发布时间】:2019-06-27 11:56:20
【问题描述】:

我正在使用 Azure IoT Hub、Python 中的 Azure IoT SDK 和一个带有温度和湿度传感器的树莓派做一个遥测应用程序。

湿度 + 温度传感器 => Rasperry Pi => Azure IoT 中心

对于我的应用程序,我使用 2 个循环线程以不同的频率发送数据: - 一个循环收集温度传感器的数据,每 60 秒将其发送到 Azure IoT Hub - 一个循环收集湿度传感器的数据,每 600 秒将其发送到 Azure IoT Hub。

我想正确关闭 2 个循环线程。它们目前正在运行,无法破坏它们。

我使用的是 Python 2.7。 我从 "threading, Thread" 库中听说过 Event,但我找不到一些可以应用的程序结构的好例子。

如何使用 Event 正确关闭线程?如何用另一种方法结束这些循环?

这是我使用 2 个线程(包括循环)的代码结构。

from threading import Thread

def send_to_azure_temperature_thread_func:
    client = iothub_client_init()
    while True:
        collect_temperature_data()
        send_temperature_data(client)
        time.sleep(60)

def send_to_humidity_thread_func():
    client = iothub_client_init()
    while True:
        collect_humidity_data()
        send_humidity_data(client)
        time.sleep(600)

if __name__ == '__main__':
    print("Threads...")
    temperature_thread = Thread(target=send_to_azure_temperature_thread_func)
    temperature_thread.daemon = True
    print("Thread1 init")

    humidity_thread = Thread(target=send_to_azure_humidity_thread_func)
    humidity_thread.daemon = True
    print("Thread2 init")

    temperature_thread.start()
    humidity_thread.start()
    print("Threads start")

    temperature_thread.join()
    humidity_thread.join()
    print("Threads wait")

【问题讨论】:

    标签: python multithreading python-2.7 python-multithreading


    【解决方案1】:

    Event 似乎是一个好方法。创建一个并将其传递给所有线程并将sleep()替换为Event.wait()并检查是否需要留下循环。

    在主线程中,可以设置事件以向线程发出信号,告知它们应该离开循环,从而结束自己。

    from threading import Event, Thread
    
    
    def temperature_loop(stop_requested):
        client = iothub_client_init()
        while True:
            collect_temperature_data()
            send_temperature_data(client)
            if stop_requested.wait(60):
                break
    
    
    def humidity_loop(stop_requested):
        client = iothub_client_init()
        while True:
            collect_humidity_data()
            send_humidity_data(client)
            if stop_requested.wait(600):
                break
    
    
    def main():
        stop_requested = Event()
    
        print('Threads...')
        temperature_thread = Thread(target=temperature_loop, args=[stop_requested])
        temperature_thread.daemon = True
        print('Thread1 init')
    
        humidity_thread = Thread(target=humidity_loop, args=[stop_requested])
        humidity_thread.daemon = True
        print('Thread2 init')
    
        temperature_thread.start()
        humidity_thread.start()
        print('Threads start')
    
        time.sleep(2000)
        stop_requested.set()
    
        temperature_thread.join()
        humidity_thread.join()
        print('Threads wait')
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 为什么使用 event.wait(60) 而不是 time.sleep(60)?我感觉事件会在 60 秒后触发,所以它不再是一个循环,对吧?我想每 60 秒及以后发送一次消息,以便在需要时触发事件(例如关闭程序)。谢谢解答!
    • 因为event.wait(60) 将在事件设置后立即停止,而time.sleep(60) 仍在等待 60 秒。这与湿度功能中的 600 秒等待更为相关。 wait() 不触发事件它等待事件被触发。
    • 太棒了!我应该如何定义 Event()? Event() 会处理任何输入吗?像鼠标点击,回车,A,B,...?
    • Eventthreading 中定义,并在main() 函数的第一行创建一个实例。并且 必须在它上面调用set() 以在你想停止线程时触发它。在示例中,我只需等待 2000 秒即可。当然你可以做一些不同的事情来确定何时触发Event
    猜你喜欢
    • 2019-05-05
    • 2014-01-24
    • 2010-09-26
    • 2013-12-06
    • 2019-06-30
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多