远程执行任意的python代码 - 可以完成吗? [英] Execute arbitrary python code remotely - can it be done?

查看:310
本文介绍了远程执行任意的python代码 - 可以完成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个拥有许多功能非常强大的电脑的网格系统。这些可以用来快速执行python函数。我的用户有很多python函数需要很长时间才能在工作站上计算,理想情况下他们希望能够在远程强大的服务器上调用某些函数,但似乎是在本地运行的。



Python有一个名为apply的旧函数 - 现在,python支持扩展调用语法(例如**参数),但现在它几乎没有用,但是我需要实现一些有用的东西像这样:
$ b $ pre $ rapply = Rapply(server_hostname)#设置连接
result = rapply(fn,args, kwargs)#远程调用函数
assert result == fn(* args,** kwargs)#作为测试,验证它是否具有期望的值。

Rapply应该是一个可以用来远程执行一些任意代码的类( fn 可以是字面上的任何东西)在远程服务器上。它将返回 rapply 函数将返回的结果。 结果应该与我在本地调用该函数时具有相同的值。



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

如果 fn 可能具有局部依赖关系:它可能是一个简单的函数,它使用一个定义在不同的类中的类模块,是否有一种封装 fn 的方法,并且所有的 fn 需要哪个不是标准库?理想的解决方案不会要求这个系统的用户对python开发有很多知识。他们只是想编写他们的函数并调用它。



为了澄清,我不想讨论使用什么样的网络协议来实现客户&服务器。我的问题是如何将一个函数及其依赖关系封装为一个可以序列化和远程执行的单个对象。



我对于在远程服务器上运行任意代码的安全含义也不感兴趣 - 我们只是说这个系统纯粹是为了研究而设计的,防火墙环境。 > PyRO (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 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 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.

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?

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.

解决方案

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.

这篇关于远程执行任意的python代码 - 可以完成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆