Skip to main content
 首页 » 编程设计

python之rpyc: root.getmodule ("module_name") 和手动返回模块引用的区别

2024年10月24日13bjzhanghao

我想使用一个只能在远程 rpyc 服务器上访问的 python 模块。以下两种访问远程机器模块的方式有区别吗:


"""在客户端:"""

  1. my_local_mod_ref = my_rpyc_connection.root.getmodule("remote_module_name")
  2. my_local_mod_ref = my_rpyc_connection.root.a_func_returning_the_module_ref()


服务器端的 """: """

def exposed_a_func_returning_the_module_ref() 
    import my_remote_module_name 
    return my_remote_module_name 

如果有区别,两种选择中哪一种更清洁或更可取?

请您参考如下方法:

下面是这个getmodule的实现:

def exposed_getmodule(self, name): 
    """imports an arbitrary module""" 
    return __import__(name, None, None, "*") 

如您所见,如果模块尚未加载到服务器中,则调用 getmodule 将其导入,并且(无论哪种方式)返回对象模块的 netref。

如果这与您的 a_func_returning_the_module_ref() 的行为相匹配,则没有区别。

我猜 getmodule 是开箱即用的,非常有用,因此您不必明确定义它(或类似的东西)来实现此目的目标。