簡體   English   中英

在 python 中處理樹莓派問題

[英]Threading in python on a raspberry pi issues

我正在嘗試運行一個 GUI 來生成線程,這些線程在樹莓派 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 編譯為 static 二進制文件時它不起作用。

答案是獲得更好的硬件。 我沒有更改我的代碼,而是購買了更新的樹莓派 4 而不是原來的樹莓派,並且線程工作就像它最初在我的另一台運行 arch 的 PC 上所做的那樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM