From 58b57068ab770d099bdd54d4eb8d22ce8a4f6751 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Thu, 5 Oct 2023 11:07:44 +0100 Subject: [PATCH] Internal code restructure (no functional changes) --- src/socketio/base_client.py | 25 ++++++++++++++++++++++++ src/socketio/client.py | 38 ------------------------------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/socketio/base_client.py b/src/socketio/base_client.py index 70a6e9c..95fea1e 100644 --- a/src/socketio/base_client.py +++ b/src/socketio/base_client.py @@ -1,5 +1,7 @@ import itertools import logging +import signal +import threading from . import base_namespace from . import packet @@ -8,6 +10,24 @@ default_logger = logging.getLogger('socketio.client') reconnecting_clients = [] +def signal_handler(sig, frame): # pragma: no cover + """SIGINT handler. + + Notify any clients that are in a reconnect loop to abort. Other + disconnection tasks are handled at the engine.io level. + """ + for client in reconnecting_clients[:]: + client._reconnect_abort.set() + if callable(original_signal_handler): + return original_signal_handler(sig, frame) + else: # pragma: no cover + # Handle case where no original SIGINT handler was present. + return signal.default_int_handler(sig, frame) + + +original_signal_handler = None + + class BaseClient: reserved_events = ['connect', 'connect_error', 'disconnect', '__disconnect_final'] @@ -16,6 +36,11 @@ class BaseClient: reconnection_delay=1, reconnection_delay_max=5, randomization_factor=0.5, logger=False, serializer='default', json=None, handle_sigint=True, **kwargs): + global original_signal_handler + if handle_sigint and original_signal_handler is None and \ + threading.current_thread() == threading.main_thread(): + original_signal_handler = signal.signal(signal.SIGINT, + signal_handler) self.reconnection = reconnection self.reconnection_attempts = reconnection_attempts self.reconnection_delay = reconnection_delay diff --git a/src/socketio/client.py b/src/socketio/client.py index 7d7036b..9f730a7 100644 --- a/src/socketio/client.py +++ b/src/socketio/client.py @@ -1,6 +1,4 @@ import random -import signal -import threading import engineio @@ -9,24 +7,6 @@ from . import exceptions from . import packet -def signal_handler(sig, frame): # pragma: no cover - """SIGINT handler. - - Notify any clients that are in a reconnect loop to abort. Other - disconnection tasks are handled at the engine.io level. - """ - for client in base_client.reconnecting_clients[:]: - client._reconnect_abort.set() - if callable(original_signal_handler): - return original_signal_handler(sig, frame) - else: # pragma: no cover - # Handle case where no original SIGINT handler was present. - return signal.default_int_handler(sig, frame) - - -original_signal_handler = None - - class Client(base_client.BaseClient): """A Socket.IO client. @@ -87,24 +67,6 @@ class Client(base_client.BaseClient): fatal errors are logged even when ``engineio_logger`` is ``False``. """ - def __init__(self, reconnection=True, reconnection_attempts=0, - reconnection_delay=1, reconnection_delay_max=5, - randomization_factor=0.5, logger=False, serializer='default', - json=None, handle_sigint=True, **kwargs): - global original_signal_handler - if handle_sigint and original_signal_handler is None and \ - threading.current_thread() == threading.main_thread(): - original_signal_handler = signal.signal(signal.SIGINT, - signal_handler) - - super().__init__(reconnection=reconnection, - reconnection_attempts=reconnection_attempts, - reconnection_delay=reconnection_delay, - reconnection_delay_max=reconnection_delay_max, - randomization_factor=randomization_factor, - logger=logger, serializer=serializer, json=json, - handle_sigint=handle_sigint, **kwargs) - def connect(self, url, headers={}, auth=None, transports=None, namespaces=None, socketio_path='socket.io', wait=True, wait_timeout=1):