1

我有少量 rpyc 服务器,它们只有部分具有相同的公开功能。

在客户端事件应该被转发到所有已经连接并且对特定事件感兴趣的服务器。

我想从服务器连接对象获取服务器上可用的公开函数列表。

到目前为止,我发现的最好的事情是使用方法名称在客户端检查现有的公开函数,例如

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

这会在客户端引发 AttributeError 异常 - 但是 - 这也会在远程服务器上引发我想避免的异常。

考虑在每个服务器上添加一个通用的“exposed_supportedFunctions”函数并返回其公开函数的列表,但这看起来有点矫枉过正并且容易出现不匹配。

4

2 回答 2

1

您可以通过获取连接对象的属性来做到这一点。在 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)) ]
于 2019-11-30T03:04:50.593 回答
1

我所做的是添加此服务

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
于 2019-10-28T08:15:27.297 回答