使用rpyc无需导入即可访问另一个文件中的Class成员

时间:2018-11-02 01:25:37

标签: python rpyc

server.py

import rpyc

class MyError(Exception):
    def __init__(self, message):
        super(MyError, self).__init__(message)
        self.error = message

    def foo1(self):
        self.error_type = 1

    def foo2(self):
        self.error_type = 2


class MyService(rpyc.Service):
    def exposed_test(self):
        e = MyError('error')
        e.foo1()
        return e


if __name__ == '__main__':
    from rpyc.utils.server import ThreadPoolServer

    server = ThreadPoolServer(MyService(), port=6000)
    server.start()

client.py中,我想基于error_type采取不同的步骤,如下所示:

import rpyc

with rpyc.connect('localhost', 6000) as conn:
    try:
        raise conn.root.test()
    except Exception as e:
        if e.error_type == 1:
            pass
        else:
            pass

但是,编译器说

Traceback (most recent call last):
  File "C:/python/client.py", line 5, in <module>
    raise conn.root.test()
TypeError: exceptions must derive from BaseException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/python/client.py", line 7, in <module>
    if e.error_type == 1:
AttributeError: 'TypeError' object has no attribute 'error_type'

如何访问此类对象的成员变量?即使我将Exception更改为BaseException也没用。

更新: 我将client.py修改为:

import rpyc

with rpyc.connect('localhost', 6000) as conn:
    e = conn.root.test()
    print(e)
    print(e.error_type)

print(e)正确,因为它显示“错误”,但我仍然无法得到e.error_type

0 个答案:

没有答案