Skip to content

Commit

Permalink
Remove weakref
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbouteiller committed Jan 6, 2024
1 parent e40426d commit f9e7b5c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
42 changes: 18 additions & 24 deletions tlspyo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tlspyo/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit f9e7b5c

Please sign in to comment.