diff --git a/tlspyo/api.py b/tlspyo/api.py index 4499213..8a5b2e0 100644 --- a/tlspyo/api.py +++ b/tlspyo/api.py @@ -4,7 +4,6 @@ from threading import Thread, Lock from multiprocessing import Process import os -import weakref from tlspyo.server import Server from tlspyo.client import Client @@ -93,11 +92,10 @@ def __init__(self, self._local_com_conn, self._local_com_addr = self._local_com_srv.accept() self._send_local('TEST') - self._finalizer = weakref.finalize(self, self._finalize) self._stop_lock = Lock() self._stopped = False - def _finalize(self): + def __del__(self): self.stop() def _send_local(self, cmd): @@ -225,14 +223,13 @@ def __init__(self, self._local_com_conn, self._local_com_addr = self._local_com_srv.accept() self._send_local(cmd='TEST') - self._t_manage_received_objects = Thread(target=self._manage_received_objects, daemon=False) + self._t_manage_received_objects = Thread(target=self._manage_received_objects, daemon=True) self._t_manage_received_objects.start() - self._finalizer = weakref.finalize(self, self._finalize) self._stop_lock = Lock() self._stopped = False - def _finalize(self): + def __del__(self): self.stop() def _deserialize(self, obj): @@ -242,25 +239,22 @@ def _manage_received_objects(self): """ Called in its own thread. """ - try: - buf = b"" - while True: - # Check if socket is still open - with self.__socket_closed_lock: - if self.__socket_closed_flag: - return - - buf += self._local_com_conn.recv(self._max_buf_len) + buf = b"" + while True: + # Check if socket is still open + with self.__socket_closed_lock: + if self.__socket_closed_flag: + return + + buf += self._local_com_conn.recv(self._max_buf_len) + i, j = self._process_header(buf) + while j <= len(buf): + stamp, cmd, obj = self._deserialize(buf[i:j]) + if cmd == "OBJ": + to_put = obj if self._deserialize_locally else self._deserialize(obj) + self.__obj_buffer.put(to_put) # TODO: maxlen + buf = buf[j:] i, j = self._process_header(buf) - while j <= len(buf): - stamp, cmd, obj = self._deserialize(buf[i:j]) - if cmd == "OBJ": - to_put = obj if self._deserialize_locally else self._deserialize(obj) - self.__obj_buffer.put(to_put) # TODO: maxlen - buf = buf[j:] - i, j = self._process_header(buf) - finally: - self._local_com_conn.close() def _process_header(self, buf): i = self._header_size diff --git a/tlspyo/utils.py b/tlspyo/utils.py index b43ead3..1b9e2d0 100644 --- a/tlspyo/utils.py +++ b/tlspyo/utils.py @@ -1,11 +1,11 @@ -import signal +# import signal import queue -try: - signal.signal(signal.SIGINT, signal.SIG_DFL) -except Exception as e: - pass +# try: +# signal.signal(signal.SIGINT, signal.SIG_DFL) +# except Exception as e: +# pass def wait_event(event):