gpt4 book ai didi

python - 串行读/写代码中可能存在的竞争条件

转载 作者:太空宇宙 更新时间:2023-11-03 18:06:46 25 4
gpt4 key购买 nike

我正在编写读取和写入串行设备的Python代码。该设备基本上是一个运行 Marlin 的 Arduino Mega 3D打印机固件。

我的 python 代码正在发送一系列 GCode 命令(以换行符结尾的 ASCII 字符串,包括校验和和行号)。 Marlin 用“ok\n”响应每个成功接收的行。 Marlin 的行缓冲区大小有限,因此如果行缓冲区已满,Marlin 将推迟发送“ok\n”响应,直到空间被释放。

如果校验和失败,则 Marlin 会请求再次发送该线路并返回“重新发送:143\n”响应。如果请求当前温度,另一种可能的响应是“ok T:{temp value}\n”。

我的代码使用三个线程。主线程,一个读线程和一个写线程。这是代码的精简版本:

class Printer:

def connect(self):
self.s = serial.Serial(self.port, self.baudrate, timeout=3)
self.ok_received.set()

def _start_print_thread(self):
self.print_thread = Thread(target=self._empty_buffer, name='Print')
self.print_thread.setDaemon(True)
self.print_thread.start()

def _start_read_thread(self):
self.read_thread = Thread(target=self._continous_read, name='Read')
self.read_thread.setDaemon(True)
self.read_thread.start()

def _empty_buffer(self):
while not self.stop_printing:
if self.current_line_idx < len(self.buffer):
while not self.ok_received.is_set() and not self.stop_printing:
logger.debug('waiting on ok_received')
self.ok_received.wait(2)
line = self._next_line()
self.s.write(line)
self.current_line_idx += 1
self.ok_received.clear()
else:
break

def _continous_read(self):
while not self.stop_reading:
if self.s is not None:
line = self.s.readline()
if line == 'ok\n':
self.ok_received.set()
continue # if we got an OK then we need to do nothing else.
if 'Resend:' in line: # example line: "Resend: 143"
self.current_line_idx = int(line.split()[1]) - 1
if line: # if we received _anything_ then set the flag
self.ok_received.set()
else: # if no printer is attached, wait 10ms to check again.
sleep(0.01)

在上面的代码中,self.ok_received是一个threading.Event。这大部分工作正常。然而,每隔几个小时,它就会陷入 _empty_buffer() 内部的 while not self.ok_received.is_set() 和 not self.stop_printing: 循环中。这会通过锁定机器来终止打印。

当卡在循环中时,我可以通过手动发送任何命令来继续打印。这允许读取线程设置 ok_recieved 标志。

由于 Marlin 不响应校验和,我猜“ok\n”可能会出现乱码。读取线程中的第三个 if 语句应该通过设置标志来处理此问题(如果从 Marlin 收到任何内容)。

所以我的问题是:我是否在某个地方可能存在竞争条件?在我到处添加锁或将两个线程合并为一个线程之前,我真的很想了解这是如何失败的。任何建议将不胜感激。

最佳答案

看起来读取线程可以在写入线程已脱离 is_set 循环的窗口中获取一些数据,但尚未调用 self.ok_received.clear() 。因此,读取线程最终会调用 self.ok_received.set(),而写入线程仍在处理上一行,然后写入线程在不知不觉中调用 clear()一旦处理完前一条消息,就永远不知道应该写入另一行。

def _empty_buffer(self):
while not self.stop_printing:
if self.current_line_idx < len(self.buffer):
while not self.ok_received.is_set() and not self.stop_printing:
logger.debug('waiting on ok_received')
self.ok_received.wait(2)
# START OF RACE WINDOW
line = self._next_line()
self.s.write(line)
self.current_line_idx += 1
# END OF RACE WINDOW
self.ok_received.clear()
else:
break

队列可能是处理此问题的好方法 - 每次读取线程接收一行时,您都希望在写入线程中写入一行。如果将 self.ok_received.set() 替换为 self.recv_queue.put("line"),则写入线程每次拉取内容时只能写入一行来自队列:

def _empty_buffer(self):
while not self.stop_printing:
if self.current_line_idx < len(self.buffer):
while not self.stop_printing:
logger.debug('waiting on ok_received')
try:
val = self.recv_queue.get(timeout=2)
except Queue.Empty:
pass
else:
break
line = self._next_line()
self.s.write(line)
self.current_line_idx += 1
else:
break

您还可以在退出内部 while 后立即将对 self.ok_received.clear() 的调用向上移动,从而将窗口缩小到实际中可能不会碰到的程度 循环,但从技术上讲,仍然会存在竞争。

关于python - 串行读/写代码中可能存在的竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26741580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com