RPyC - 从暴露的继承 class

RPyC - inherit from exposed class

我有一个导入特定模块的 RpyC 服务器,这个模块中的一个 class 应该暴露出来,这样这个 class 就可以从客户端继承。

出于测试目的,我删除了模块 importing/exposing 并在我的 RPyC 服务中创建了一个简单的 class,名为 exposed_TestClass。

服务器端:rpyc_server.py

import rpyc
from rpyc.utils.server import ThreadedServer

class MyService(rpyc.Service):
    class exposed_TestClass:
        def exposed_Exec(self):
            print("original print of class")

t = ThreadedServer(MyService, port=12345)
t.start()

客户端:python3shell

>>> import rpyc
>>> conn = rpyc.connect("localhost", 12345)
>>> conn.root.TestClass
<class 'exposed_TestClass'>
>>> conn.root.TestClass()
<exposed_TestClass object at 0x7f2dda642588>
>>> #calling the Exec function also works, prints at server side
>>> conn.root.TestClass().Exec()
>>>
>>>
>>> # test inheriting class
>>> class MyClass(conn.root.TestClass):
...     def NewMethod(self):
...         print("printing from new method")
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/rpyc/core/netref.py", line 220, in method
    return syncreq(_self, consts.HANDLE_CALLATTR, name, args, kwargs)
  File "/usr/lib/python3/dist-packages/rpyc/core/netref.py", line 74, in syncreq
    conn = object.__getattribute__(proxy, "____conn__")
AttributeError: 'str' object has no attribute '____conn__'

根据以下文档:https://rpyc.readthedocs.io/en/latest/tutorial/tut2.html

Voila, netrefs (network references, also known as transparent object proxies) are special objects that delegate everything done on them locally to the corresponding remote objects. Netrefs may not be real lists of functions or modules, but they “do their best” to look and feel like the objects they point to… in fact, they even fool python’s introspection mechanisms!

在客户端,如果您这样做:

import rpyc
conn = rpyc.connect("localhost", 12345)

o=conn.root.TestClass()
type_o = type(o)
print(type_o)

它将打印出:

<netref class 'rpyc.core.netref.__main__.exposed_TestClass'>

所以当尝试做的时候:

class MyClass(type_o):
    def NewMethod(self):
        print("printing from new method")

您从代理 class 定义继承,但不是从远程 class 定义继承。所以当文档说明

they “do their best”

,我认为它排除了从远程 class 进行继承的可能性。如果有人能找到满足您要求的方法,我将非常高兴。

请注意,我通过使用以下行设置服务器来进行测试:

t = ThreadedServer(MyService, port=12345, protocol_config={ 'allow_all_attrs': True })