Commit 90791768 authored by David Wilson's avatar David Wilson

issue #155: core: slightly rearrange how shutdown works

This eliminates Context.on_disconnect() and instead moves its
functionality to a signal wired up by ExternalContext.main().

It leaves mitogen.master.Context is in a better condition to move into
mitogen.parent where it belongs.
parent 4f93c682
...@@ -832,10 +832,6 @@ class Stream(BasicStream): ...@@ -832,10 +832,6 @@ class Stream(BasicStream):
be called from any thread.""" be called from any thread."""
self._router.broker.defer(self._send, msg) self._router.broker.defer(self._send, msg)
def on_disconnect(self, broker):
super(Stream, self).on_disconnect(broker)
self._router.on_disconnect(self, broker)
def on_shutdown(self, broker): def on_shutdown(self, broker):
"""Override BasicStream behaviour of immediately disconnecting.""" """Override BasicStream behaviour of immediately disconnecting."""
_v and LOG.debug('%r.on_shutdown(%r)', self, broker) _v and LOG.debug('%r.on_shutdown(%r)', self, broker)
...@@ -862,9 +858,8 @@ class Context(object): ...@@ -862,9 +858,8 @@ class Context(object):
return _unpickle_context, (self.context_id, self.name) return _unpickle_context, (self.context_id, self.name)
def on_disconnect(self, broker): def on_disconnect(self, broker):
_v and LOG.debug('Parent stream is gone, dying.') _v and LOG.debug('%r.on_disconnect()', self)
fire(self, 'disconnect') fire(self, 'disconnect')
broker.shutdown()
def on_shutdown(self, broker): def on_shutdown(self, broker):
pass pass
...@@ -1118,8 +1113,7 @@ class Router(object): ...@@ -1118,8 +1113,7 @@ class Router(object):
def __repr__(self): def __repr__(self):
return 'Router(%r)' % (self.broker,) return 'Router(%r)' % (self.broker,)
def on_disconnect(self, stream, broker): def on_stream_disconnect(self, stream):
"""Invoked by Stream.on_disconnect()."""
for context in self._context_by_id.itervalues(): for context in self._context_by_id.itervalues():
stream_ = self._stream_by_id.get(context.context_id) stream_ = self._stream_by_id.get(context.context_id)
if stream_ is stream: if stream_ is stream:
...@@ -1138,6 +1132,7 @@ class Router(object): ...@@ -1138,6 +1132,7 @@ class Router(object):
self._stream_by_id[context.context_id] = stream self._stream_by_id[context.context_id] = stream
self._context_by_id[context.context_id] = context self._context_by_id[context.context_id] = context
self.broker.start_receive(stream) self.broker.start_receive(stream)
listen(stream, 'disconnect', lambda: self.on_stream_disconnect(stream))
def add_handler(self, fn, handle=None, persist=True, respondent=None): def add_handler(self, fn, handle=None, persist=True, respondent=None):
handle = handle or self._last_handle.next() handle = handle or self._last_handle.next()
...@@ -1359,6 +1354,10 @@ class ExternalContext(object): ...@@ -1359,6 +1354,10 @@ class ExternalContext(object):
return return
self.broker.shutdown() self.broker.shutdown()
def _on_parent_disconnect(self):
_v and LOG.debug('%r: parent stream is gone, dying.', self)
self.broker.shutdown()
def _setup_master(self, profiling, parent_id, context_id, in_fd, out_fd): def _setup_master(self, profiling, parent_id, context_id, in_fd, out_fd):
self.profiling = profiling self.profiling = profiling
if profiling: if profiling:
...@@ -1378,6 +1377,7 @@ class ExternalContext(object): ...@@ -1378,6 +1377,7 @@ class ExternalContext(object):
self.stream.accept(in_fd, out_fd) self.stream.accept(in_fd, out_fd)
self.stream.receive_side.keep_alive = False self.stream.receive_side.keep_alive = False
listen(self.stream, 'disconnect', self._on_parent_disconnect)
listen(self.broker, 'shutdown', self._on_broker_shutdown) listen(self.broker, 'shutdown', self._on_broker_shutdown)
listen(self.broker, 'exit', self._on_broker_exit) listen(self.broker, 'exit', self._on_broker_exit)
......
...@@ -644,13 +644,6 @@ class Broker(mitogen.core.Broker): ...@@ -644,13 +644,6 @@ class Broker(mitogen.core.Broker):
class Context(mitogen.core.Context): class Context(mitogen.core.Context):
via = None via = None
def on_disconnect(self, broker):
"""
Override base behaviour of triggering Broker shutdown on parent stream
disconnection.
"""
mitogen.core.fire(self, 'disconnect')
def call_async(self, fn, *args, **kwargs): def call_async(self, fn, *args, **kwargs):
LOG.debug('%r.call_async(%r, *%r, **%r)', LOG.debug('%r.call_async(%r, *%r, **%r)',
self, fn, args, kwargs) self, fn, args, kwargs)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment