[execnet-dev] Persistent sessions

holger krekel holger at merlinux.eu
Mon Oct 14 22:47:49 CEST 2013


On Mon, Oct 14, 2013 at 13:40 -0700, Rick Gerkin wrote:
> Is there anyway to make the contents of the interpreter persist between
> calls to remote_exec?  For example,
> 
> import execnet
> gw = execnet.makegateway("popen//python=jython")
> ch1 = gw.remote_exec('var=2; channel.send(var)')
> ch1.receive() # Prints 2.
> ch2 = gw.remote_exec('channel.send(var)')
> ch2.receive() # NameError: name 'var' is not defined.
> 
> I would expect the gateway to correspond to one continuous python session,
> but I guess it doesn't?

Each remote_exec() has a separate namespace.  There is no execnet-offered
"sharing" space that survives calls.  We could think about something.
Meanwhile you can stick things on an imported module, for example
"sys" or "os" if you don't have own ones.  Or write a little
support code that puts an artifical module into sys.modules,
like this:

    def get_shared_data():
        name = "shared_data"
        try:
            return sys.modules[name]
        except KeyError:
            mod = sys.modules[name] = types.ModuleType(name)
            l.append(mod)
            return mod

You can then use the result of get_shared_data() to store/retrieve
attributes across sessions.

holger


More information about the execnet-dev mailing list