Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save Averroes/9ac1c207c472b0d35570 to your computer and use it in GitHub Desktop.
implementing remote procedure call
# rpcserver.py
import pickle
class RPCHandler:
def __init__(self):
self._functions = { }
def register_function(self, func):
self._functions[func.__name__] = func
def handle_connection(self, connection):
try:
while True:
# Receive a message
func_name, args, kwargs = pickle.loads(connection.recv())
# Run the RPC and send a response
try:
r = self._functions[func_name](*args,**kwargs)
connection.send(pickle.dumps(r))
except Exception as e:
connection.send(pickle.dumps(e))
except EOFError:
pass
# Example use
from multiprocessing.connection import Listener
from threading import Thread
def rpc_server(handler, address, authkey):
sock = Listener(address, authkey=authkey)
while True:
client = sock.accept()
t = Thread(target=handler.handle_connection, args=(client,))
t.daemon = True
t.start()
# Some remote functions
def add(x, y):
return x + y
def sub(x, y):
return x - y
# Register with a handler
handler = RPCHandler()
handler.register_function(add)
handler.register_function(sub)
# Run the server
rpc_server(handler, ('localhost', 17000), authkey=b'peekaboo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment