【问题标题】:Exposing a bunch of functions through Pyro通过 Pyro 暴露一堆函数
【发布时间】:2017-06-26 12:41:21
【问题描述】:

我有一个名为 foobar 的模块,它包含一堆我想远程调用的函数。

我目前的解决方案是将所有这些函数作为静态方法包装在一个类中并共享。

这是我的代码:

pyro_server.py:

import Pyro4
import foobar

import inspect
Pyro4.config.REQUIRE_EXPOSE = False

import my_custom_pyro_config as pyro_config

def module_to_class(module):
    class Wrapper:
        pass
    for name, func in inspect.getmembers(module, inspect.isfunction):
        setattr(Wrapper, name, staticmethod(func))
    return Wrapper

def main():
    name_server = Pyro4.locateNS(host=pyro_config.IP, port=pyro_config.NS_PORT)
    daemon = Pyro4.Daemon(host=pyro_config.IP, port=pyro_config.PYRO_PORT)
    foobar_uri = daemon.register(module_to_class(foobar))
    name_server.register("foobar", foobar_uri)
    print("Entering request loop")
    daemon.requestLoop()

它有效,但感觉有点狡猾......

有没有更好的方法来做到这一点?我愿意切换到另一个 RPC 库

【问题讨论】:

    标签: python rpc pyro pyro4


    【解决方案1】:

    使用 Pyro 的“Flame”功能。它允许直接远程访问服务器上的模块,而无需手动公开成员。

    >>> import Pyro4.utils.flame
    >>> Pyro4.config.SERIALIZER="pickle"
    >>> fl = Pyro4.utils.flame.connect("localhost:55225")  # use whatever location the flame server is running on
    >>> s = fl.module("sys")
    >>> s.stdout.write("foobar\n")
    7    
    # ...and observe 'foobar' being written to the server's output
    

    【讨论】:

    • 感谢厄门。我想我没有想到使用火焰,因为它太强大/不安全/可怕
    • 是的,这就是为什么将要公开的内容显式包装在一个类中并导出这是安全路线的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2019-06-20
    • 1970-01-01
    相关资源
    最近更新 更多