aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/gcode.py
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-06-09 12:01:58 -0400
committerKevin O'Connor <kevin@koconnor.net>2019-06-09 18:44:44 -0400
commit962f7b98bd3bafe5ffefc00e22e95a7fab377a10 (patch)
tree922bec5878f500131d0befaa3137d8504d89e748 /klippy/gcode.py
parentafc10400e342cdd29f362d0f8b03af607b62abda (diff)
downloadkutter-962f7b98bd3bafe5ffefc00e22e95a7fab377a10.tar.gz
kutter-962f7b98bd3bafe5ffefc00e22e95a7fab377a10.tar.xz
kutter-962f7b98bd3bafe5ffefc00e22e95a7fab377a10.zip
gcode: Convert input handling to use a reactor mutex
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/gcode.py')
-rw-r--r--klippy/gcode.py36
1 files changed, 8 insertions, 28 deletions
diff --git a/klippy/gcode.py b/klippy/gcode.py
index 947bb469..b740a1e9 100644
--- a/klippy/gcode.py
+++ b/klippy/gcode.py
@@ -31,6 +31,7 @@ class GCodeParser:
self.input_log = collections.deque([], 50)
# Command handling
self.is_printer_ready = False
+ self.mutex = self.reactor.mutex()
self.base_gcode_handlers = self.gcode_handlers = {}
self.ready_gcode_handlers = {}
self.mux_commands = {}
@@ -267,34 +268,20 @@ class GCodeParser:
return
# Process commands
self.is_processing_data = True
- self.pending_commands = []
- self._process_commands(pending_commands)
- if self.pending_commands:
- self._process_pending()
- self.is_processing_data = False
- def _process_pending(self):
- pending_commands = self.pending_commands
while pending_commands:
self.pending_commands = []
- self._process_commands(pending_commands)
+ with self.mutex:
+ self._process_commands(pending_commands)
pending_commands = self.pending_commands
+ self.is_processing_data = False
if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd,
self._process_data)
def process_batch(self, commands):
- if self.is_processing_data:
+ if self.mutex.test():
return False
- self.is_processing_data = True
- try:
+ with self.mutex:
self._process_commands(commands, need_ack=False)
- except self.error as e:
- if self.pending_commands:
- self._process_pending()
- self.is_processing_data = False
- raise
- if self.pending_commands:
- self._process_pending()
- self.is_processing_data = False
return True
def run_script_from_command(self, script):
prev_need_ack = self.need_ack
@@ -303,15 +290,8 @@ class GCodeParser:
finally:
self.need_ack = prev_need_ack
def run_script(self, script):
- commands = script.split('\n')
- curtime = None
- while 1:
- res = self.process_batch(commands)
- if res:
- break
- if curtime is None:
- curtime = self.reactor.monotonic()
- curtime = self.reactor.pause(curtime + 0.100)
+ with self.mutex:
+ self._process_commands(script.split('\n'), need_ack=False)
# Response handling
def ack(self, msg=None):
if not self.need_ack or self.is_fileinput: