🚧 Wait for disconnect until all's been processed

This commit is contained in:
Gina Häußge committed 2019-08-01 13:12:15 +02:00
1 parent 16cff1a098
commit 0612fe4e36
5 files changed
+27 -10

No files matched your search

+8 -2
View File
@@ -185,7 +185,7 @@ class Protocol(ListenerAware, TransportListener):
self._transport.connect(*transport_args, **transport_kwargs)
self.state = ProtocolState.CONNECTING
def disconnect(self, error=False):
def disconnect(self, error=False, wait=True, timeout=10.0):
if self.state in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR, ProtocolState.DISCONNECTING, ProtocolState.DISCONNECTING_WITH_ERROR):
raise ProtocolNotConnectedError("Already disconnecting or disconnected")
@@ -197,10 +197,13 @@ class Protocol(ListenerAware, TransportListener):
self.process_protocol_log("--- Protocol {} disconnecting from transport {}...".format(self,
self._transport))
if wait:
self.join(timeout=timeout)
self._transport.unregister_listener(self)
if self._transport.state == TransportState.CONNECTED:
try:
self._transport.disconnect()
self._transport.disconnect(wait=wait)
except Exception:
self._logger.exception("Error while disconnecting from transport {}".format(self._transport))
error = True
@@ -262,6 +265,9 @@ class Protocol(ListenerAware, TransportListener):
def repair(self):
pass
def join(self, timeout=None):
pass
def on_job_started(self, job, *args, **kwargs):
self.notify_listeners("on_protocol_job_started", self, job, *args, **kwargs)
self.state = ProtocolState.PROCESSING
+14 -3
View File
@@ -1005,6 +1005,15 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
def repair(self, *args, **kwargs):
self._on_comm_ok()
def join(self, timeout=None):
if timeout is not None:
stop = monotonic_time() + timeout
while (self._command_queue.unfinished_tasks or self._send_queue.unfinished_tasks) and monotonic_time() < stop:
time.sleep(0.1)
else:
self._command_queue.join()
self._send_queue.join()
##~~ State handling
def _is_operational(self):
@@ -1283,7 +1292,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
if self._internal_flags["dwelling_until"] and monotonic_time() > self._internal_flags["dwelling_until"]:
self._internal_flags["dwelling_until"] = False
if self.state == ProtocolState.CONNECTING:
if self.state == ProtocolState.CONNECTING and len(line):
hello = self.flavor.command_hello()
if hello:
self._tickle(hello)
@@ -1553,8 +1562,10 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
self._clear_to_send.set()
else:
self._to_logfile_with_terminal("WARNING! Received an error from the printer's firmware, ignoring that as configured "
"but you might want to investigate what happened here! Error: {}".format(error))
message = "--- WARNING! Received an error from the printer's firmware, ignoring that as configured " \
"but you might want to investigate what happened here! Error: {}".format(error)
self._to_logfile_with_terminal(message, level=logging.WARNING)
self.process_protocol_log(message)
self._clear_to_send.set()
def _on_error_communication(self, error_type):
+3 -3
View File
@@ -117,11 +117,11 @@ class Transport(ListenerAware):
self.state = TransportState.CONNECTED
self.notify_listeners("on_transport_connected", self)
def disconnect(self, error=None):
def disconnect(self, error=None, wait=True):
if self.state == TransportState.DISCONNECTED:
raise TransportNotConnectedError("Already disconnected")
success = self.drop_connection()
success = self.drop_connection(wait=wait)
if not success and not error:
error = "Error disconnecting transport"
@@ -136,7 +136,7 @@ class Transport(ListenerAware):
def create_connection(self, *args, **kwargs):
return True
def drop_connection(self):
def drop_connection(self, wait=True):
return True
def read(self, size=None, timeout=None):
@@ -130,7 +130,7 @@ class SerialTransport(Transport):
return True
def drop_connection(self):
def drop_connection(self, wait=True):
error = False
if self._serial is not None:
@@ -39,7 +39,7 @@ class TcpTransport(Transport):
return True
def drop_connection(self):
def drop_connection(self, wait=True):
try:
self._socket.close()
return True