Last active
August 29, 2015 14:00
-
-
Save curzona/11134889 to your computer and use it in GitHub Desktop.
Remote procedure call with execnet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import execnet | |
| import sys | |
| # Boilerplate | |
| def remote_call(gw, method, args, kargs): | |
| module = sys.modules[__name__] | |
| channel = gw.remote_exec(module) | |
| channel.send((method, args, kargs)) | |
| result = channel.receive() | |
| return result | |
| class _Method(): | |
| def __init__(self, gw, name): | |
| self.gw = gw | |
| self.name = name | |
| def __call__(self, *args, **kargs): | |
| return remote_call(self.gw, self.name, args, kargs) | |
| class ExecnetProxy(): | |
| def __init__(self, *args, **kargs): | |
| self.gw = execnet.makegateway(*args, **kargs) | |
| def __del__(self): | |
| self.gw.exit() | |
| def __getattr__(self, name): | |
| return _Method(self.gw, name) | |
| def serve_forver(channel): | |
| while not channel.isclosed(): | |
| funcname, args, kargs = channel.receive() | |
| retval = globals()[funcname](*args, **kargs) | |
| channel.send(retval) | |
| # Fun stuff starts here | |
| def greetings(name): | |
| return "hello %s" % (name) | |
| if __name__ == '__channelexec__': | |
| serve_forver(channel) | |
| if __name__ == '__main__': | |
| proxy = ExecnetProxy() | |
| for name in ["Joey", "Alfonso"]: | |
| print proxy.greetings(name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment