From da8e72b0d6a47603c70c148d5c36e39890594eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 12 Nov 2014 12:32:58 +0100 Subject: [PATCH] Use a bounded semaphore for properly limiting the max count of items in the queue, also track removed items upon send_queue.clear --- src/octoprint/comm/protocol/reprap/__init__.py | 15 +++++++++++---- src/octoprint/comm/protocol/reprap/util.py | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index 1e4ceca02..860deaef2 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -133,7 +133,7 @@ class RepRapProtocol(Protocol): self._send_queue = CommandQueue() - self._clear_for_send = CountedEvent(max=20) + self._clear_for_send = CountedEvent(max=10) self._force_checksum = True self._wait_for_start = False @@ -159,7 +159,7 @@ class RepRapProtocol(Protocol): self._thread.daemon = True self._thread.start() - self._fill_queue_semaphore = threading.Semaphore(20) + self._fill_queue_semaphore = threading.BoundedSemaphore(10) self._fill_queue_state_signal = threading.Event() self._fill_queue_mutex = threading.Lock() self._fill_queue_processing = True @@ -280,7 +280,8 @@ class RepRapProtocol(Protocol): Protocol.cancel_print(self) with self._fill_queue_mutex: - self._send_queue.clear(matcher=lambda entry: entry is not None and entry.command is not None and hasattr(entry.command, "progress") and entry.command.progress is not None and (not hasattr(entry, "prepared") or entry.prepared is None)) + cleared = self._send_queue.clear(matcher=lambda entry: entry is not None and entry.command is not None and hasattr(entry.command, "progress") and entry.command.progress is not None and (not hasattr(entry, "prepared") or entry.prepared is None)) + self._logger.debug("Cleared %d entries from the send queue: %r" % (len(cleared), cleared)) def init_sd(self): Protocol.init_sd(self) @@ -872,7 +873,13 @@ class RepRapProtocol(Protocol): self._logger.debug("Dropping command which was disabled through preprocessing: %s" % item.command) # remove from send queue - self._fill_queue_semaphore.release() + try: + self._fill_queue_semaphore.release() + except ValueError: + # that's ok, the bounded semaphore complains that we just released more often than we acquired, but + # we do this explicitly an just use the bounded semaphore to guarantee an upper bound on the + # items added to the send queue by the fill thread + pass self._send_queue.remove(entry) return True diff --git a/src/octoprint/comm/protocol/reprap/util.py b/src/octoprint/comm/protocol/reprap/util.py index 22dc0c679..b99e7a35c 100644 --- a/src/octoprint/comm/protocol/reprap/util.py +++ b/src/octoprint/comm/protocol/reprap/util.py @@ -227,12 +227,15 @@ class CommandQueue(Queue.Queue): if matcher is None: matcher = lambda x: True + cleared = [] with self.mutex: for entry in self.queue[:]: priority, counter, item = entry if matcher(item): self._del(entry, heapify=False) + cleared.append(entry) heapq.heapify(self.queue) + return cleared def __repr__(self): return repr(self.queue)