0
我正在設計一個基於QT的應用程序,它是用Python設計的。該應用程序具有以下兩個按鈕:在Python 2.7中立即停止線程執行/終止
- 移動機器人
- 停止機器人
機器人需要一些時間來移動從一點到另一點。因此,我調用一個新的線程來控制機器人的運動,以防止GUI無響應。移動功能下方:
from threading import Thread
from thread import start_new_thread
def move_robot(self):
def move_robot_thread(points):
for point in points:
thread = Thread(target=self.robot.move, args=(point,))
thread.start()
thread.join()
start_new_thread(move_robot_thread, (points,))
上述功能運行良好。爲了阻止機器人的運動,我需要停止執行上述線程。請參閱下面的完整代碼:
from python_qt_binding.QtGui import QPushButton
self.move_robot_button = QPushButton('Move Robot')
self.move_robot_button.clicked.connect(self.move_robot)
self.move_robot_button = QPushButton('Stop Robot')
self.move_robot_button.clicked.connect(self.stop_robot)
self.robot = RobotControllerWrapper()
from threading import Thread
from thread import start_new_thread
def move_robot(self):
def move_robot_thread(points):
for point in points:
thread = Thread(target=self.robot.move, args=(point,))
thread.start()
thread.join()
start_new_thread(move_robot_thread, (points,))
def stop_robot(self):
pass
class RobotControllerWrapper():
def __init__(self):
self.robot_controller = RobotController()
def move(self, point):
while True:
self._robot_controller.move(point)
current_location = self.robot_controller.location()
if current_location - point < 0.0001:
break
如何停止執行線程?有什麼建議嗎?