[英]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.