Python 设置标志后,线程不会停止

Python 设置标志后,线程不会停止,python,multithreading,Python,Multithreading,我正在用Python中的Tkinter为控制两个移动轴的串行设备创建GUI。我已经设置了一个tkinter窗口,包含按钮、条目等 该项目的一个关键部分是在单击按钮时停止轴的移动。让我们看一下这个例子:我可以通过串口发送命令来测量一个轴的绝对位置,但是这个测量过程可能需要两分钟,因此我的整个程序(包括GUI)等待串行设备反馈位置。我需要它等待响应,但是我的GUI应该仍然能够响应发出stop-move命令。我开始使用多线程,但没有成功。似乎线程没有停止 以下是代码片段: global cPMthre

我正在用Python中的Tkinter为控制两个移动轴的串行设备创建GUI。我已经设置了一个tkinter窗口,包含按钮、条目等

该项目的一个关键部分是在单击按钮时停止轴的移动。让我们看一下这个例子:我可以通过串口发送命令来测量一个轴的绝对位置,但是这个测量过程可能需要两分钟,因此我的整个程序(包括GUI)等待串行设备反馈位置。我需要它等待响应,但是我的GUI应该仍然能够响应发出stop-move命令。我开始使用多线程,但没有成功。似乎线程没有停止

以下是代码片段:

global cPMthread
cPMthread = threading.Thread(target = doMeasuring, args = (getPort(), axis, steps))
cPMthread.start()
当按下开始测量的按钮时,将调用此命令

def doMeasuring(port, axis, *therest):
  global cPMthread
  cPMthread = threading.currentThread()
  if getattr(cPMthread, "do_run", True):
      currentPosition = Agilis.measureCurrentPosition(port, axis)
这将启动新线程

def stopMove(port, axis):
  global cPMthread
  print(cPMthread, "trying to stop the process")
  cPMthread.do_run = False
在这里,我尝试设置标志来停止这个过程


感谢您提供的任何建议

“线程未停止。”:不要在此处重新定义:
cPMthread=threading.currentThread()
。这是usless:
如果getattr(cPMthread,“do_run”,True):
将始终计算为
True
。是
Agilis.measureCurrentPosition
阻塞吗?感谢您的回复。但这并没有停止这个过程。你说的阻塞是什么意思?“你说的阻塞是什么意思?”:意思是永远运行,例如在
中,而True:
循环中。Read@X_841“无法使它工作。”:你实现了这个:
而getattr(t,“do_run”,True):
inside
measureCurrentPosition
?在玩了一会儿之后,我终于把语句放在了正确的位置,它现在似乎正在工作。非常感谢你的帮助!“线程未停止。”:不要在此处重新定义:
cPMthread=threading.currentThread()
。这是usless:
如果getattr(cPMthread,“do_run”,True):
将始终计算为
True
。是
Agilis.measureCurrentPosition
阻塞吗?感谢您的回复。但这并没有停止这个过程。你说的阻塞是什么意思?“你说的阻塞是什么意思?”:意思是永远运行,例如在
中,而True:
循环中。Read@X_841“无法使它工作。”:你实现了这个:
而getattr(t,“do_run”,True):
inside
measureCurrentPosition
?在玩了一会儿之后,我终于把语句放在了正确的位置,它现在似乎正在工作。非常感谢你的帮助!