From 7a50fa724eddc99d0c5b27e66ed8777ca0ffe383 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sat, 26 Aug 2017 15:44:57 +0530 Subject: [PATCH] Defer stream naming until after connect, so PID can be included. --- econtext/master.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/econtext/master.py b/econtext/master.py index 34409e14..a57b8ed7 100644 --- a/econtext/master.py +++ b/econtext/master.py @@ -335,7 +335,6 @@ class Stream(econtext.core.Stream): remote_name = '%s@%s:%d' remote_name %= (getpass.getuser(), socket.gethostname(), os.getpid()) self.remote_name = remote_name - self.name = 'local.default' self.debug = debug def on_shutdown(self, broker): @@ -398,6 +397,7 @@ class Stream(econtext.core.Stream): def connect(self): LOG.debug('%r.connect()', self) pid, fd = self.create_child(*self.get_boot_command()) + self.name = 'local.%s' % (pid,) self.receive_side = econtext.core.Side(self, fd) self.transmit_side = econtext.core.Side(self, os.dup(fd)) LOG.debug('%r.connect(): child process stdin/stdout=%r', @@ -412,7 +412,7 @@ class Stream(econtext.core.Stream): def _connect_bootstrap(self): discard_until(self.receive_side.fd, 'EC0\n', time.time() + 10.0) - return self._ec0_received() + self._ec0_received() class Broker(econtext.core.Broker): @@ -486,6 +486,16 @@ def _proxy_connect(econtext, name, context_id, klass, kwargs): class Router(econtext.core.Router): context_id_counter = itertools.count(1) + debug = False + + def enable_debug(self): + """ + Cause this context and any descendant child contexts to write debug + logs to /tmp/econtext..log. + """ + econtext.core.enable_debug_logging() + self.debug = True + def __enter__(self): return self @@ -493,14 +503,6 @@ class Router(econtext.core.Router): self.broker.shutdown() self.broker.join() - def _connect(self, context_id, klass, name=None, **kwargs): - context = Context(self, context_id) - stream = klass(self, context.context_id, context.key, **kwargs) - context.name = name or stream.name - stream.connect() - self.register(context, stream) - return context - def sudo(self, **kwargs): import econtext.sudo return self.connect(econtext.sudo.Stream, **kwargs) @@ -509,15 +511,13 @@ class Router(econtext.core.Router): import econtext.ssh return self.connect(econtext.ssh.Stream, **kwargs) - debug = False - - def enable_debug(self): - """ - Cause this context and any descendant child contexts to write debug - logs to /tmp/econtext..log. - """ - econtext.core.enable_debug_logging() - self.debug = True + def _connect(self, context_id, klass, name=None, **kwargs): + context = Context(self, context_id) + stream = klass(self, context.context_id, context.key, **kwargs) + stream.connect() + context.name = name or stream.name + self.register(context, stream) + return context def connect(self, klass, name=None, **kwargs): kwargs.setdefault('debug', self.debug)