Skip to content

Commit

Permalink
improve socket shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-piekarski committed Sep 28, 2023
1 parent 0300a39 commit b7de2e2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 4 additions & 0 deletions examples/example_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ def _process_message(self, obj):

for c in cPids:
c.close()

time.sleep(5)
logger.warning("Stopping server")
server.stop()
server.join()
logger.warning("Example script exited")
20 changes: 12 additions & 8 deletions jsocket/jsocket_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
logger = logging.getLogger("jsocket")

class JsonSocket(object):
def __init__(self, address='127.0.0.1', port=5489):
def __init__(self, address='127.0.0.1', port=5489, timeout=2.0):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.conn = self.socket
self._timeout = None
self._timeout = timeout
self._address = address
self._port = port

Expand Down Expand Up @@ -74,17 +74,21 @@ def read_obj(self):
return json.loads(str(msg[0],'ascii'))

def close(self):
logger.debug("closing all connections")
self._close_connection()
self._close_socket()
if self.socket is not self.conn:
self._close_connection()


def _close_socket(self):
logger.debug("closing main socket")
self.socket.close()
if self.socket.fileno() != -1:
self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()

def _close_connection(self):
logger.debug("closing the connection socket")
self.conn.close()
if self.conn.fileno() != -1:
self.conn.shutdown(socket.SHUT_RDWR)
self.conn.close()

def _get_timeout(self):
return self._timeout
Expand Down Expand Up @@ -152,7 +156,7 @@ def connect(self):
return True
return False


if __name__ == "__main__":
""" basic json echo server """
import threading
Expand Down
7 changes: 3 additions & 4 deletions jsocket/tserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def run(self):
logger.exception(e)
self._close_connection()
break
self.close()
self._close_socket()

def start(self):
""" Starts the threaded server.
Expand Down Expand Up @@ -124,11 +124,10 @@ def run(self):
logger.debug("socket.timeout: %s" % e)
continue
except Exception as e:
logger.info("client connection broken, closing socket")
self._close_connection()
logger.info("client connection broken, exit and close connection socket")
self._isAlive = False
break
self.close()
self._close_connection()

def start(self):
""" Starts the factory thread.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup, Extension

setup(name='jsocket',
version='1.9.1',
version='1.9.2',
description='Python JSON Server & Client',
author='Christopher Piekarski',
author_email='[email protected]',
Expand Down

0 comments on commit b7de2e2

Please sign in to comment.