我想运行并杀死按钮上的线程

时间:2019-08-27 14:46:10

标签: python multithreading tkinter

我有一个程序,该程序应该通过串行连接向arduino发送一些数据点,该arduino将控制一些电动机的运动。我可以单独发送控制信号,也可以通过txt文件发送控制信号,该文件将重复运行直到文件完成。运行txt文件时,我希望能够像暂停或停止按钮一样退出循环。我认为最好的方法是通过我可以关闭的线程。我以前从未做过任何线程处理,而且我的基本尝试没有奏效。这是发送文件数据的功能。

def send_file():
    # Global vars
    global moto1pos
    global motor2pos
    # Set Ready value
    global isready
    # Get File location
    program_file_name = file_list.get('active')
    file_path = "/home/evan/Documents/bar_text_files/"
    program_file = Path(file_path + program_file_name)
    file = open(program_file)
    pos1 = []
    pos2 = []
    speed1 = []
    speed2 = []
    accel1 = []
    accel2 = []
    for each in file:
        vals = each.split()
        pos1.append(int(vals[0]))
        pos2.append(int(vals[1]))
        speed1.append(int(vals[2]))
        speed2.append(int(vals[3]))
        accel1.append(int(vals[4]))
        accel2.append(int(vals[5]))
    # Send file values
    try:
        while isready == 1:
            for i in range(len(pos1)):
                print("Step: " + str(i+1))
                data = struct.pack("!llhhhh", pos1[i], pos2[i], speed1[i], speed2[i], accel1[i], accel2[i])
                ser.write(data)

                try:
                    pos1time = abs(pos1[i]/speed1[i])
                except:
                    pos1time = 0
                try:
                    pos2time = abs(pos2[i]/speed2[i])
                except:
                    pos2time = 0
            time_array = (pos1time, pos2time)
            time.sleep(max(time_array))
            motor1pos = ser.readline()
            motor2pos = ser.readline()
            if i < (len(pos1)-1):
                isready = ord(ser.read(1))
            else:
                isready = 0
except:
    print("Error: data not sent. Check serial port is open")

这是我希望sendfile命令从中运行的线程命令。

def thread():
    try:
        global isready
        isready = 1
        t = threading.Thread(name='sending_data', target=command)
        t.start()
    except:
        print("Threading Error: you don't know what you are doing")

这是我希望线程被杀死的停止功能:

def stop():
    try:
        global isready
        isready = 0
        t.kill()
    except:
        print("Error: thread wasn't killed")

我知道您不应该杀死线程,但是数据不是很重要。更重要的是在出现故障之前停止电动机。

tkinter中的按钮是:

run_file_butt = tk.Button(master = file_frame, text = "Run File", command = thread)

当我单击按钮时,程序会运行,但是停止功能无法停止运动。

2 个答案:

答案 0 :(得分:0)

  

问题:运行并杀死按钮上的线程

没有这样一个叫做.kill(...的东西。
开始将def send_file(...设为Thread object,它正在等待命令。

  

注意:就目前而言,内部while isready == 1:不会因使用m.set_state('stop')而停止。
  强制必须在其中启动Thread对象:

if __name__ == '__main__':
    m = MotorControl()
import threading, time

class MotorControl(threading.Thread):
    def __init__(self):
        super().__init__()
        self.state = {'is_alive'}
        self.start()

    def set_state(self, state):
        if state == 'stop':
            state = 'idle'

        self.state.add(state)

    def terminate(self):
        self.state = {}

    # main function in a Thread object
    def run(self):
        # Here goes your initalisation
        # ...

        while 'is_alive' in self.state:

            if 'start' in self.state:
                isready = 1
                while isready == 1:
                    # Here goes your activity
                    # Simulate activity
                    print('running')
                    time.sleep(2)
                    isready = 0

                self.state = self.state - {'start'}
                self.state.add('idle')

            elif 'idle' in self.state:
                print('idle')
                time.sleep(1)

if __name__ == '__main__':
    m = MotorControl()

    time.sleep(2)
    m.set_state('start')
    time.sleep(3)
    m.set_state('stop')
    time.sleep(3)
    m.set_state('start')

    time.sleep(4)
    m.terminate()
    print('EXIT __main__')

您的tk.Button应该如下所示:

tk.Button(text = "Run File", command = lambda:m.set_state('start'))
tk.Button(text = "Stop File", command = lambda:m.set_state('stop'))
tk.Button(text = "Terminate", command = m.terminate)

答案 1 :(得分:0)

由于对线程的简单理解以及使用线程的独特情况,我得出的答案很简单。我没有以我希望的方式终止线程,而是向send_file函数的发送行添加了另一个条件语句。

    while isready == 1:
        for i in range(len(pos1)):
            if motorstop == False:
                print("Step: " + str(i+1))
                #data = struct.pack('!llllhhhhhhhh', pos1[i], pos2[i], pos3[i], pos4[i], speed1[i], speed2[i], speed3[i], speed[4], accel1[i], accel2[i], accel3[i], accel4[i])
                data = struct.pack("!llhhhh", pos1[i], pos2[i], speed1[i], speed2[i], accel1[i], accel2[i])
                ser.write(data)
            else:
                isready = 0
                break

并且我已经将stop()函数更新为以下内容:

def stop():
    try:
        global motorstop
        global t
        motorstop = True
        t.join()
    except:
        print("Error: thread wasn't killed")

我不确定它是如何工作的,但是它比@stovefl提到的要简单得多。

使用此代码,由于该函数主要处于睡眠状态,因此可以运行,但不会发送任何新信息,然后在下一次迭代后将显示.join()。