Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 10, 2015 17:15
Show Gist options
  • Select an option

  • Save Averroes/aa78e25cd663683cd59b to your computer and use it in GitHub Desktop.

Select an option

Save Averroes/aa78e25cd663683cd59b to your computer and use it in GitHub Desktop.
implementing remote procedure call
import pickle
class RPCProxy:
def __init__(self, connection):
self._connection = connection
def __getattr__(self, name):
def do_rpc(*args, **kwargs):
self._connection.send(pickle.dumps((name, args, kwargs)))
result = pickle.loads(self._connection.recv())
if isinstance(result, Exception):
raise result
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:
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