Skip to content

Commit

Permalink
Internal code restructure (no functional changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 5, 2023
1 parent ef0f88f commit 58b5706
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
25 changes: 25 additions & 0 deletions src/socketio/base_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import itertools
import logging
import signal
import threading

from . import base_namespace
from . import packet
Expand All @@ -8,6 +10,24 @@
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']
Expand All @@ -16,6 +36,11 @@ 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)
self.reconnection = reconnection
self.reconnection_attempts = reconnection_attempts
self.reconnection_delay = reconnection_delay
Expand Down
38 changes: 0 additions & 38 deletions src/socketio/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import random
import signal
import threading

import engineio

Expand All @@ -9,24 +7,6 @@
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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 58b5706

Please sign in to comment.