Create new thread if last thread is finished [Python 2.7]

时间:2017-11-13 06:30:00

标签: python multithreading qt

I am new to Python threads. Basically, I have a QT based application designed in Python. The application has a button "Follow Trajectory", whenever I press this button, the robot starts moving. I noticed that robot is not following the exact trajectory.

enter image description here

The move command is being overwritten and robot receives only a few points. Below is the function, which gets executed when the button is pressed:

def move_robot(self):
    # points is an array of point
    for point in points:
        start_new_thread(self.robot.move, (point,))

Below is the move function from the controller:

def move(self, point):
    while True:
        self._robot_controller.move(point)
        current_location = self._robot_controller.location()
        if current_location - point < 0.0001:
            break

My question is that how to create a new thread only if last thread is finished?

Below is the complete code:

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

from thread import start_new_thread
from qt_gui.plugin import Plugin
from python_qt_binding.QtGui import QWidget, QPushButton

class MyPlugin(Plugin):
    def __init__(self, context):

        super(MyPlugin, self).__init__(context)
        self.setObjectName('MyPlugin')

        self._widget = QWidget()
        self._vertical_layout = QVBoxLayout()
        self._move_robot_button = QPushButton('Follow Trajectory')
        self._move_robot_button.clicked.connect(self.move_robot)
        self._vertical_layout.addWidget(self._move_robot_button)
        self._widget.setLayout(self._vertical_layout)
        self._widget.setObjectName('MyPluginUi')

        if context.serial_number() > 1:
            self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
        context.add_widget(self._widget)

        self.robot = RobotControllerWrapper()

    def move_robot(self):
        # points is an array of point
        for point in points:
            start_new_thread(self.robot.move, (point,))

Apologize for the long post. Thank you very much.

2 个答案:

答案 0 :(得分:1)

根据您的评论,您需要为def move_robot(self): # points is an array of point def move_robot_thread(points): for point in points: thread = start_new_thread(self.robot.move, (point,)) thread.join() start_new_thread(move_robot_thread, (points,))

创建新主题
start_new_thread

顺便说一句,我认为Thread会返回def move_robot(self): from threading import Thread, start_new_thread # points is an array of point 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,)) 个对象。

更新

function readShapeText() {
   var presentation = SlidesApp.getActivePresentation();
   var slides = presentation.getSlides();

   for (i = 0; i < slides.length; i++) {

       if(slides[i].getObjectId() == 'mySlideId'){

         var pageElement = slides[i].getPageElements()[0].asShape().getText().asString();

       }
   }

   var myModifiedElement = "My_" + pageElement + "_is_cool";
}

这是给你的。我没有测试过,但它应该可以工作。

答案 1 :(得分:1)

在方法移动机器人的定义中,您应该添加一个“线程控制器”来检查上一个正在运行的线程是否结束,然后再开始新线程。 这可能是一个很好的教程,Python: is thread still running

特别是方法:

if last_thread.isAlive() ==  False:
    new_thread = start_new_thread(self.robot.move, (point,))