Skip to content

Commit

Permalink
libev_connection: use globals to track sigint and sigterm watchers as…
Browse files Browse the repository at this point in the history
… they are created globally within libev
  • Loading branch information
Michael Laing committed Sep 9, 2013
1 parent b3588f0 commit 22ada59
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions pika/adapters/libev_connection.py
Expand Up @@ -8,6 +8,8 @@
from pika.adapters.base_connection import BaseConnection

LOGGER = logging.getLogger(__name__)
global_sigint_watcher = None
global_sigterm_watcher = None

class LibevConnection(BaseConnection):
"""The LibevConnection runs on the libev IOLoop. If you're running the
Expand Down Expand Up @@ -70,7 +72,7 @@ class LibevConnection(BaseConnection):
_LIBEV_TO_PIKA_ARRAY[
pyev.EV_READ|pyev.EV_WRITE
] = BaseConnection.READ|BaseConnection.WRITE

def __init__(
self, parameters=None,
on_open_callback=None,
Expand All @@ -86,19 +88,19 @@ def __init__(
:param pika.connection.Parameters parameters: Connection parameters
:param on_open_callback: The method to call when the connection is open
:type on_open_callback: method
:param on_open_error_callback: Method to call if the connection cant
:param on_open_error_callback: Method to call if the connection cannot
be opened
:type on_open_error_callback: method
:param bool stop_ioloop_on_close: Call ioloop.stop() if disconnected
:param custom_ioloop: Override using the default IOLoop in libev
Only allow on_signal_callback to one custom_ioloop
:param on_signal_callback: Method to call if SIGINT or SIGTERM occur
:type on_signal_callback: method
"""

self.ioloop = custom_ioloop or pyev.default_loop()
self._on_signal_callback = on_signal_callback
self._sigint_watcher = None
self._sigterm_watcher = None
self._io_watcher = None
self._active_timers = {}
self._stopped_timers = deque()
Expand All @@ -123,14 +125,18 @@ def _adapter_connect(self):
error = super(LibevConnection, self)._adapter_connect()

if not error:
if self._on_signal_callback and not self._sigterm_watcher:
self._sigterm_watcher = self.ioloop.signal(
if self._on_signal_callback and not global_sigterm_watcher:
global global_sigterm_watcher

global_sigterm_watcher = self.ioloop.signal(
signal.SIGTERM,
self._handle_sigterm
)

if self._on_signal_callback and not self._sigint_watcher:
self._sigint_watcher = self.ioloop.signal(
if self._on_signal_callback and not global_sigint_watcher:
global global_sigint_watcher

global_sigint_watcher = self.ioloop.signal(
signal.SIGINT,
self._handle_sigint
)
Expand All @@ -142,8 +148,8 @@ def _adapter_connect(self):
self._handle_events
)

if self._on_signal_callback: self._sigterm_watcher.start()
if self._on_signal_callback: self._sigint_watcher.start()
if self._on_signal_callback: global_sigterm_watcher.start()
if self._on_signal_callback: global_sigint_watcher.start()
self._io_watcher.start()

return error
Expand All @@ -155,8 +161,8 @@ def _init_connection_state(self):
"""
for timer in self._active_timers: self.remove_timeout(timer)
if self._sigint_watcher: self._sigint_watcher.stop()
if self._sigterm_watcher: self._sigterm_watcher.stop()
if global_sigint_watcher: self._sigint_watcher.stop()
if global_sigterm_watcher: self._sigterm_watcher.stop()
if self._io_watcher: self._io_watcher.stop()
super(LibevConnection, self)._init_connection_state()

Expand Down

0 comments on commit 22ada59

Please sign in to comment.