关于树莓派问题的python线程

时间:2019-10-03 17:11:05

标签: python multithreading raspberry-pi

我正在尝试运行一个GUI,该GUI生成的线程在树莓pi 1上执行非常基本的任务,而不是计算复杂的任务,而且我似乎无法使这些线程正常工作。

我在x86英特尔计算机上开发了代码,它运行得很好。基本上,线程命令只允许按下按钮并同时侦听串行数据。

    def extra_thread_disable():
    # Disables buttons that would interfere with data that is currently being sent
    while threading.active_count() == 3:
        run_file_butt.config(state = 'disabled')
        run_butt.config(state = 'disabled')
        serial_butt.config(state = 'disabled')
        popup_butt.config(state = 'disabled')
        homing_butt.config(state = 'disabled')
        level_butt.config(state = 'disabled')
        zero_button1.config(state = 'disabled')
        zero_button2.config(state = 'disabled')
        zero_button3.config(state = 'disabled')
    else:
        run_file_butt.config(state = 'normal')
        run_butt.config(state = 'normal')
        serial_butt.config(state = 'normal')
        popup_butt.config(state = 'normal')
        homing_butt.config(state = 'normal')
        level_butt.config(state = 'normal')
        zero_button1.config(state = 'normal')
        zero_button2.config(state = 'normal')
        zero_button3.config(state = 'normal')
    pass



def thread_data():
    # Starts a thread to send data while allowing stop button to be pressed
    try:
        global t2
        t2 = threading.Thread(name='send_line', target = send_data, daemon = True)
        t_disable = threading.Thread(name='disable', target = extra_thread_disable, daemon = True)
        t2.start()
        t_disable.start()
    except:
        update_textbox("Threading Error: data thread not properly created")



def send_data():
    # Sends single motion commands and waits for response to continue
    global save_path
    global motor_param
    vals = get_vals()
    try:
        data = struct.pack("!llllhhhhhhhh", vals['dist1'], vals['dist2'], vals['dist34'], vals['dist34'], vals['speed1'], vals['speed2'], vals['speed34'], vals['speed34'], vals['accel1'], vals['accel2'], vals['accel34'], vals['accel34'])
        try:
            ser.write(data)
            update_textbox("Running...")
        except:
            update_textbox("Error: Data not sent")
        try:
            motor1pos = int(ser.readline())
            motor2pos = int(ser.readline())
            motor3pos = int(ser.readline())
            motor4pos = int(ser.readline())
            ready = ser.read(1)
            update_textbox("Movement complete")
            axis1_current.set(str(reverse_convert(motor1pos, 1)))
            axis2_current.set(str(reverse_convert(motor2pos, 2)))
            axis3_current.set(str(reverse_convert(motor3pos, 3)))
            writetofile()
        except:
            update_textbox("Error: reading data improperly")
    except:
        update_textbox("Error: data not sent properly")
    pass

代码基本上只允许主GUI线程允许按下停止按钮,并禁用所有可能干扰发送数据的按钮。然后,该线程仅等待来自其连接的arduino的响应。同样,所有这些都可以在普通计算机上正常运行。在树莓派上运行时,在终端上没有收到任何错误或警告,但似乎被阻止了。我以为这可能只是一台速度缓慢的计算机或臭名昭著的GIL。看来这可能是原因。如果是这样,我应该切换到python中的多处理库吗?有办法解决这个问题吗?在终端中调用python3运行时,它不起作用;使用pyinstaller将其编译为静态二进制文件时,它也不起作用。

1 个答案:

答案 0 :(得分:-1)

答案是获得更好的硬件。我没有对代码进行任何更改,而是购买了较新的raspberry pi 4而不是原始的raspberry pi,并且该线程的工作原理与我在其他运行PC的计算机上的工作原理相同。