Skip to content

Commit ea84b9b

Browse files
Option to disable the SIGINT handler in the client (Fixes miguelgrinberg#792)
1 parent db0565a commit ea84b9b

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ packages = find:
2525
python_requires = >=3.6
2626
install_requires =
2727
bidict >= 0.21.0
28-
python-engineio >= 4.1.0
28+
python-engineio >= 4.3.0
2929

3030
[options.packages.find]
3131
where = src

src/socketio/asyncio_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class AsyncClient(client.Client):
4040
packets. Custom json modules must have ``dumps`` and ``loads``
4141
functions that are compatible with the standard library
4242
versions.
43+
:param handle_sigint: Set to ``True`` to automatically handle disconnection
44+
when the process is interrupted, or to ``False`` to
45+
leave interrupt handling to the calling application.
46+
Interrupt handling can only be enabled when the
47+
client instance is created in the main thread.
4348
4449
The Engine.IO configuration supports the following settings:
4550

src/socketio/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class Client(object):
6868
packets. Custom json modules must have ``dumps`` and ``loads``
6969
functions that are compatible with the standard library
7070
versions.
71+
:param handle_sigint: Set to ``True`` to automatically handle disconnection
72+
when the process is interrupted, or to ``False`` to
73+
leave interrupt handling to the calling application.
74+
Interrupt handling can only be enabled when the
75+
client instance is created in the main thread.
7176
7277
The Engine.IO configuration supports the following settings:
7378
@@ -90,9 +95,9 @@ class Client(object):
9095
def __init__(self, reconnection=True, reconnection_attempts=0,
9196
reconnection_delay=1, reconnection_delay_max=5,
9297
randomization_factor=0.5, logger=False, serializer='default',
93-
json=None, **kwargs):
98+
json=None, handle_sigint=True, **kwargs):
9499
global original_signal_handler
95-
if original_signal_handler is None and \
100+
if handle_sigint and original_signal_handler is None and \
96101
threading.current_thread() == threading.main_thread():
97102
original_signal_handler = signal.signal(signal.SIGINT,
98103
signal_handler)
@@ -101,8 +106,10 @@ def __init__(self, reconnection=True, reconnection_attempts=0,
101106
self.reconnection_delay = reconnection_delay
102107
self.reconnection_delay_max = reconnection_delay_max
103108
self.randomization_factor = randomization_factor
109+
self.handle_sigint = handle_sigint
104110

105111
engineio_options = kwargs
112+
engineio_options['handle_sigint'] = handle_sigint
106113
engineio_logger = engineio_options.pop('engineio_logger', None)
107114
if engineio_logger is not None:
108115
engineio_options['logger'] = engineio_logger

tests/common/test_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ def test_create(self, engineio_client_class):
2929
reconnection_delay=5,
3030
reconnection_delay_max=10,
3131
randomization_factor=0.2,
32+
handle_sigint=False,
3233
foo='bar',
3334
)
3435
assert not c.reconnection
3536
assert c.reconnection_attempts == 123
3637
assert c.reconnection_delay == 5
3738
assert c.reconnection_delay_max == 10
3839
assert c.randomization_factor == 0.2
39-
engineio_client_class().assert_called_once_with(foo='bar')
40+
assert not c.handle_sigint
41+
engineio_client_class().assert_called_once_with(
42+
foo='bar', handle_sigint=False)
4043
assert c.connection_url is None
4144
assert c.connection_headers is None
4245
assert c.connection_transports is None
@@ -89,7 +92,8 @@ def test_logger(self):
8992
@mock.patch('socketio.client.Client._engineio_client_class')
9093
def test_engineio_logger(self, engineio_client_class):
9194
client.Client(engineio_logger='foo')
92-
engineio_client_class().assert_called_once_with(logger='foo')
95+
engineio_client_class().assert_called_once_with(
96+
handle_sigint=True, logger='foo')
9397

9498
def test_on_event(self):
9599
c = client.Client()

0 commit comments

Comments
 (0)