pypy / pypy / tool /

import exceptions, os
from pypy.tool import slaveproc

class IsolateException(Exception):

class IsolateInvoker(object):
    # to have a nice repr
    def __init__(self, isolate, name):
        self.isolate = isolate = name

    def __call__(self, *args):
        return self.isolate._invoke(, args)
    def __repr__(self):
        return "<invoker for %r . %r>" % (self.isolate.module,

    def close_isolate(self):

class Isolate(object):
    Isolate lets load a module in a different process,
    and support invoking functions from it passing and
    returning simple values

    module: a dotted module name or a tuple (directory, module-name)
    _closed = False

    def __init__(self, module):
        self.module = module
        self.slave = slaveproc.SlaveProcess(os.path.join(os.path.dirname(__file__),
        res = self.slave.cmd(('load', module))
        assert res == 'loaded'

    def __getattr__(self, name):
        return IsolateInvoker(self, name)

    def _invoke(self, func, args):
        status, value = self.slave.cmd(('invoke', (func, args)))
        print 'OK'
        if status == 'ok':
            return value
            exc_type_module, exc_type_name = value
            if exc_type_module == 'exceptions':
                raise getattr(exceptions, exc_type_name)
                raise IsolateException, "%s.%s" % value 

    def _close(self):
        if not self._closed:
            self._closed = True

    def __del__(self):

def close_isolate(isolate):
    assert isinstance(isolate, Isolate)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.