issue #76: reduce Context duplication during unpickling

When unpickling a context, arrange for there to be a single instance
representing that context, managed by the corresponding router. This
context_by_id() was already in use by parent.py, it just needs to move
down.

This to eventually reach the point where a single Context exists that
needs 'disconnect' fired on it, so all sleeping receivers are definitely
woken.
This commit is contained in:
David Wilson 2018-10-23 22:16:09 +01:00
parent 72da291b24
commit d7d40f1123
2 changed files with 10 additions and 10 deletions

View File

@ -1279,7 +1279,7 @@ def _unpickle_context(router, context_id, name):
(isinstance(name, UnicodeType) and len(name) < 100))
):
raise TypeError('cannot unpickle Context: bad input')
return router.context_class(router, context_id, name)
return router.context_by_id(context_id, name=name)
class Poller(object):
@ -1732,6 +1732,15 @@ class Router(object):
_, (_, func, _) = self._handle_map.popitem()
func(Message.dead())
def context_by_id(self, context_id, via_id=None, create=True, name=None):
context = self._context_by_id.get(context_id)
if create and not context:
context = self.context_class(self, context_id, name=name)
if via_id is not None:
context.via = self.context_by_id(via_id)
self._context_by_id[context_id] = context
return context
def register(self, context, stream):
_v and LOG.debug('register(%r, %r)', context, stream)
self._stream_by_id[context.context_id] = stream

View File

@ -1581,15 +1581,6 @@ class Router(mitogen.core.Router):
def allocate_id(self):
return self.id_allocator.allocate()
def context_by_id(self, context_id, via_id=None, create=True):
context = self._context_by_id.get(context_id)
if create and not context:
context = self.context_class(self, context_id)
if via_id is not None:
context.via = self.context_by_id(via_id)
self._context_by_id[context_id] = context
return context
connection_timeout_msg = u"Connection timed out."
def _connect(self, klass, name=None, **kwargs):