简体   繁体   中英

Threading in python on a raspberry pi issues

I am trying to run a GUI that spawns threads that perform very basic and not computationally complicated tasks on a raspberry pi 1 and I cannot seem to get the threads to work.

I developed the code on a x86 intel computer and it works perfectly well. The threading commands basically just allow for button presses and listening for serial data concurrently.

    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

The code basically just allows the main GUI thread to allow for a stop button to be pressed and disable all the buttons that could interfere with the sent data. That thread then just waits for the response from an arduino it is connected to. Again this all works flawlessly on a normal computer. I get no errors or warnings in the terminal when run on a raspberry pi but it seems to be blocking. I thought maybe it was just such a slow computer or the infamous GIL. It seems like that might be the reason. If so, should I switch to the multiprocessing library in python? is there a way to get around this? It doesn't work when run in terminal calling python3 and it doesn't work when it was compiled to a static binary using pyinstaller.

The answer is to get better hardware. I changed nothing in my code but instead bought a newer raspberry pi 4 instead of the original raspberry pi and the threading worked as it originally did on my other pc running arch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM