Created
April 10, 2015 17:14
-
-
Save Averroes/bfdaf9b0c596746ca3c2 to your computer and use it in GitHub Desktop.
implementing remote procedure call
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 json | |
| class RPCProxy: | |
| def __init__(self, connection): | |
| self._connection = connection | |
| def __getattr__(self, name): | |
| def do_rpc(*args, **kwargs): | |
| self._connection.send(json.dumps((name, args, kwargs))) | |
| result = json.loads(self._connection.recv()) | |
| return result | |
| return do_rpc | |
| # Example use | |
| from multiprocessing.connection import Client | |
| c = Client(('localhost', 17000), authkey=b'peekaboo') | |
| proxy = RPCProxy(c) | |
| print(proxy.add(2, 3)) | |
| print(proxy.sub(2, 3)) | |
| try: | |
| print(proxy.sub([1, 2], 4)) | |
| except Exception as e: | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment