远程执行任意 python 代码 - 可以吗?

2022-01-18 00:00:00 python grid

问题描述

我正在研究一个网格系统,它有许多非常强大的计算机.这些可用于非常快速地执行 python 函数.我的用户有许多 python 函数需要很长时间才能在工作站上计算,理想情况下他们希望能够在远程强大的服务器上调用一些函数,但让它看起来在本地运行.

I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server, but have it appear to be running locally.

Python 有一个名为apply"的旧函数 - 现在 Python 支持扩展调用语法(例如 **arguments),现在它几乎没用了,但是我需要实现一些像这样工作的东西:

Python has an old function called "apply" - it's mostly useless these days now that python supports the extended-call syntax (e.g. **arguments), however I need to implement something that works a bit like this:

rapply = Rapply( server_hostname ) # Set up a connection
result = rapply( fn, args, kwargs ) # Remotely call the function
assert result == fn( *args, **kwargs ) #Just as a test, verify that it has the expected value. 

Rapply 应该是一个类,可用于在远程服务器上远程执行一些任意代码(fn 可以是任何字面意思).它将发回 rapply 函数将返回的结果.结果"的值应该与我在本地调用该函数的值相同.

Rapply should be a class which can be used to remotely execute some arbitrary code (fn could be literally anything) on a remote server. It will send back the result which the rapply function will return. The "result" should have the same value as if I had called the function locally.

现在让我们假设 fn 是一个用户提供的函数,我需要某种方式将它通过网络发送到执行服务器.如果我能保证 fn 总是很简单,它可能只是一个包含 python 源代码的字符串......但如果它不是那么简单呢?

Now let's suppose that fn is a user-provided function I need some way of sending it over the wire to the execution server. If I could guarantee that fn was always something simple it could could just be a string containing python source code... but what if it were not so simple?

如果 fn 可能具有本地依赖项怎么办:它可能是一个简单的函数,它使用在不同模块中定义的类,是否有封装 fn 和所有内容的方法fn 需要哪个不是标准库?一个理想的解决方案不需要这个系统的用户对 python 开发有太多的了解.他们只是想编写他们的函数并调用它.

What if fn might have local dependencies: It could be a simple function which uses a class defined in a different module, is there a way of encapsulating fn and everything that fn requires which is not standard-library? An ideal solution would not require the users of this system to have much knowledge about python development. They simply want to write their function and call it.

澄清一下,我没有兴趣讨论可以使用哪种网络协议来实现客户端和客户端之间的通信.服务器.我的问题是如何将函数及其依赖项封装为可以序列化和远程执行的单个对象.

Just to clarify, I'm not interested in discussing what kind of network protocol might be used to implement the communication between the client & server. My problem is how to encapsulate a function and its dependencies as a single object which can be serialized and remotely executed.

我也对在远程服务器上运行任意代码的安全隐患不感兴趣——我们只是说这个系统纯粹是为了研究而设计的,它位于一个防火墙严密的环境中.

I'm also not interested in the security implications of running arbitrary code on remote servers - let's just say that this system is intended purely for research and it is within a heavily firewalled environment.


解决方案

看看 PyRO(Python 远程对象)它能够在集群中的所有计算机上设置服务,并通过名称服务器和发布-订阅机制直接或间接调用它们.

Take a look at PyRO (Python Remote objects) It has the ability to set up services on all the computers in your cluster, and invoke them directly, or indirectly through a name server and a publish-subscribe mechanism.

相关文章