rpyc - how can I get a list of exposed functions of a connection

679 Views Asked by At

I have a small number of rpyc servers that only partially have the same exposed functions.

On the client events should be forwarded to all servers that have connected and are interested in the specific events.

I would like to get a list of available exposed functions on the servers from the servers connection object.

Best thing I found so far is checking in the client for an existing exposed function using the method name, e.g.

try:
    conn.root.exposed_recordLog
except Exception as e:
    print(f"recordLog is not exposed: {str(e)}")

which raises an AttributeError exception in the client - however - this raises also an exception on the remote server that I would like to avoid.

Thought about adding a general 'exposed_supportedFunctions' function on each server and return a list of its exposed functions but that looks like a bit of overkill and prone to mismatches.

2

There are 2 best solutions below

0
On

What I did was add this service

def exposed_get_methods(self):
    service_index = dir(self)
    exposed_methods = [x.replace('exposed_', '') for x in service_index if x.startswith('exposed_')]
    return exposed_methods
0
On

You can do this by getting the attributes of the connection object. Tested on Python 3.7.

c = rpyc.connect_by_service('myservice')
exposed_methods = [ x[8:] for x in dir(c.root) if x.startswith('exposed_') and callable(getattr(c.root, x)) ]