Python 的 RPC 世界中的远程对象返回另一个远程对象

发布于 2024-10-11 03:22:26 字数 414 浏览 6 评论 0原文

我基本上熟悉Python中可用的RPC解决方案:XML-RPC和Pyro。我可以通过在服务器端绑定它来创建一个远程对象,然后我可以在客户端获取可以操作的代理对象。当我在远程对象上调用某些方法(例如 proxy.get_file() )时,rpc 机制会尝试序列化结果对象(在本例中为文件)。这通常是预期的行为,但我需要的是获取一个文件对象作为另一个远程代理对象,而不是将其传输到客户端:

afile_proxy = proxy.get_file()

而不是:

afile = proxy.get_file()

我可以在服务器端重新绑定此对象并在客户端处理这种情况但这需要一些样板代码。有没有一个机制/库可以为我做这件事?例如,它可以使对象保持远程状态,直到它们成为原始对象为止。

I am basically familiar with RPC solutions available in Python: XML-RPC and Pyro. I can make an remote object by binding it on the server-side and then I can get proxy object on the client side on which I can operate. When I call some method on remote object e.g. proxy.get_file() then the rpc mechanism tries to serialize a resultant object (a file in this case). This is usually expected behavior, but what I need is to get a file object as another remote proxy object instead of getting it transferred to client side:

afile_proxy = proxy.get_file()

Instead of:

afile = proxy.get_file()

I could rebind this object on server-side and handle such case on the client side but this would require some boiler-plate code. Is there a mechanism/library that would do this for me? It could for example keep objects remote until they are primitive ones.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素染倾城色 2024-10-18 03:22:26

我找到了一个完全满足我需要的库:RPyC。来自 intro

  • 简单、不可变的 Python 对象(如字符串、整数、元组等)按值传递,意味着值本身被传递给另一方。
  • 所有其他对象都是通过引用传递的,这意味着对该对象的“引用”被传递到另一端。这允许对引用对象应用的更改反映在实际对象上。

无论如何,感谢您指出“参考”术语。 :)

I have found a library that does exactly what I need: RPyC. From intro:

  • simple, immutable python objects (like strings, integers, tuples, etc.) are passed by value, meaning the value itself is passed to the other side.
  • all other objects are passed by reference, meaning a "reference" to the object is passed to the other side. This allows changes applied on the referenced object to be reflected on the actual object.

Anyway, thanks for pointing out a 'reference' term. :)

江南月 2024-10-18 03:22:26

我参与开发一个新的远程对象交互框架 Versile Python (VPy),它执行与您列出的那些。 VPy 正在开发中,当前版本主要用于测试,但请随意查看。

有两种方法可以执行您使用 VPy 描述的远程 I/O 类型。一种是使用远程 本机对象 引用,例如类似的文件对象Pyro/RPyC 并访问这些对象,就像它们是本地对象一样。

另一种选择是使用 VPy 远程 stream 框架,这是相当有效的灵活,可以配置为执行双向流和操作,例如远程重新定位或截断流。第二种方法的优点是它支持异步 I/O,并且流框架分割数据传输以减少往返延迟的影响。

I am involved in developing a new remote-object interaction framework Versile Python (VPy) which performs similar functions as the ones you have listed. VPy is in development with the current releases primarily intended for testing, however feel free to take a look.

There are two ways you could perform the type of remote I/O you are describing with VPy. One is to use remote native object references to e.g. file objects similar to Pyro/RPyC and access those objects similar to if they were local.

Another option is to use the VPy remote stream framework which is quite flexible and can be configured to perform bi-directional streaming and operations such as remotely repositioning or truncating the stream. The second approach has the advantage it enables asynchronous I/O plus the stream framework splits up data transmission in order to reduce the effects of round-trip latency.

深海少女心 2024-10-18 03:22:26
afile_proxy = proxy.get_file_proxy()

并在 API 中定义什么是 FileProxy 对象。这完全取决于客户端需要使用代理做什么。葛名字?删除它吗?获取其内容?

如果您只想跟踪稍后要处理的内容,您甚至可以不用引用(也许是 URL)。这就是在网络上对所有嵌入内容(例如图像)所做的事情。

afile_proxy = proxy.get_file_proxy()

And define in the API what a FileProxy object is. It all depends on what the client needs to do with the proxy. Ge the name? Delete it? Get its contents?

You could even get away with a reference (a URL, maybe) if all you want is to keep track of something you want to process later. It's what's done on the web with all embedded content, like images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文