Skip to content

Commit

Permalink
Option to disable the SIGINT handler in the client
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 25, 2021
1 parent 8a0e4c3 commit 14ed9f1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/engineio/asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class AsyncClient(client.Client):
skip SSL certificate verification, allowing
connections to servers with self signed certificates.
The default is ``True``.
:param handle_sigint: Set to ``True`` to automatically handle disconnection
when the process is interrupted, or to ``False`` to
leave interrupt handling to the calling application.
Interrupt handling can only be enabled when the
client instance is created in the main thread.
"""
def is_asyncio_based(self):
return True
Expand Down Expand Up @@ -85,9 +90,8 @@ async def connect(self, url, headers=None, transports=None,
await eio.connect('http://localhost:5000')
"""
global async_signal_handler_set
if not async_signal_handler_set and \
if self.handle_sigint and not async_signal_handler_set and \
threading.current_thread() == threading.main_thread():

try:
asyncio.get_event_loop().add_signal_handler(
signal.SIGINT, async_signal_handler)
Expand Down
16 changes: 9 additions & 7 deletions src/engineio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ class Client(object):
skip SSL certificate verification, allowing
connections to servers with self signed certificates.
The default is ``True``.
:param handle_sigint: Set to ``True`` to automatically handle disconnection
when the process is interrupted, or to ``False`` to
leave interrupt handling to the calling application.
Interrupt handling can only be enabled when the
client instance is created in the main thread.
"""
event_names = ['connect', 'disconnect', 'message']

def __init__(self,
logger=False,
json=None,
request_timeout=5,
http_session=None,
ssl_verify=True):
def __init__(self, logger=False, json=None, request_timeout=5,
http_session=None, ssl_verify=True, handle_sigint=True):
global original_signal_handler
if original_signal_handler is None and \
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)
Expand All @@ -89,6 +90,7 @@ def __init__(self,
self.ping_interval = None
self.ping_timeout = None
self.http = http_session
self.handle_sigint = handle_sigint
self.ws = None
self.read_loop_task = None
self.write_loop_task = None
Expand Down
2 changes: 1 addition & 1 deletion tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_logger(self):
c = client.Client(logger=my_logger)
assert c.logger == my_logger

def test_custon_timeout(self):
def test_custom_timeout(self):
c = client.Client()
assert c.request_timeout == 5
c = client.Client(request_timeout=27)
Expand Down

0 comments on commit 14ed9f1

Please sign in to comment.