【问题标题】:How to stop continuous_threading in python如何在python中停止continuous_threading
【发布时间】:2021-11-23 00:44:46
【问题描述】:

我正在做一个项目,我必须从串行端口读取值并将它们显示在 tkinter GUI 上。我正在使用python的连续线程模块。我正在使用一个连续线程在每 0.5 秒后连续读取串行端口上的数据,但现在我想停止这个连续线程。那我应该怎么阻止呢?
这是按下检查按钮时我正在调用的函数

def scan():
    print("in scan")
    btn1_state = var1.get()
    print("Scan: %d"%btn1_state)
    t1 = continuous_threading.PeriodicThread(0.5, readserial)
    if(btn1_state == 1):
        t1.start()
    else:
        print("entered else ")
        t1.stop()  #I am using stop() but the thread doesn't stop

请帮忙

【问题讨论】:

    标签: python multithreading tkinter python-multithreading pyserial


    【解决方案1】:

    问题可能是您在readserial 函数中使用了阻塞读取函数。它需要超时。我可以用这段代码重现:

    import time
    import continuous_threading
    
    
    time_list = []
    
    def save_time():
        while True:
            time.sleep(1)
        time_list.append(time.time())
    
    th = continuous_threading.PeriodicThread(0.5, save_time)
    th.start()
    
    time.sleep(4)
    th.join()
    
    print(time_list)
    

    这永远不会退出。

    修改自examples

    由于 Continuous_threading 期望 它的事件循环处于控制之中,它永远不会到达停止事件。

    【讨论】:

      猜你喜欢
      • 2018-03-14
      • 2019-08-05
      • 2022-11-09
      • 2020-05-28
      • 2013-11-19
      • 2013-08-03
      • 2015-10-28
      • 1970-01-01
      相关资源
      最近更新 更多