如何为远程沙箱执行嵌入式python模块? [英] how to do an embedded python module for remote sandbox execution?

查看:79
本文介绍了如何为远程沙箱执行嵌入式python模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将python代码动态添加到沙箱模块,以便在远程计算机上执行。我遇到了如何处理导入方法的问题。例如,通常会看到如下脚本:

I am trying to dynamically add python code to a sandbox module for executing on a remote machine. I am experiencing an issue with how to deal with imported methods. For example, it is common to see scripts written such as:

 from test_module import g
 import other_module

 def f():
     g()
     other_module.z()

我知道我可以用g和可能的z来腌制f,但是如何保留z的other_module作用域?如果我将f和g都放在沙箱中,那么当f被调用时z不会被正确解析。是否有可能使用某种类型的嵌入式模块来正确解析z,即sandbox.other_module?

I know I can pickle f with g and potentially z but how do I preserve the "other_module" scope for z? If I put both f and g in the sandbox then z is not going to be resolved properly when f is called. Is it possible to use some type of embedded module to get z resolved correctly, i.e. sandbox.other_module?

将远程代码加载到沙盒中的目的是不污染全局命名空间。例如,如果另一个远程方法使用它自己的依赖关系图进行调用,那么它不应该干扰另一组远程代码。期望python在沙盒模块进入和退出使用时稳定是否现实?我这样说是因为这篇文章:
如何卸载(重新加载)一个Python模块?
这让我觉得在这种情况下删除模块(如不同的沙箱)可能会有问题。

My purpose for loading remote code into a sandbox is to not pollute the global namespace. For instance, if another remote method is invoked with it's own dependency graph then it should not interfere with another set of remote code. Is it realistic to expect python to be stable with sandbox modules coming in and out of usage? I say this because of this post: How do I unload (reload) a Python module? which makes me feel it can be problematic removing modules such as different sandboxes in this case.

推荐答案

其他模块可以导入到 sandbox (您的意思是在运行时动态创建的模块)

Other modules can be imported to sandbox (you mean modules that are created dynamically at runtime) by

    sandbox.other_module = __import__('other_module')

或:

    exec 'import other_module' in sandbox.__dict__

如果您从其他模块或其他沙箱模块中调用沙盒模块,并且您想稍后重新加载一些新代码,则仅导入模块更容易,而不是像从沙箱导入f,并调用sandbox.f而不是f。然后重新加载容易。 (但naturarely重新加载命令是没有用的)

If you call "sandbox" modules from other modules or other sandbox modules and you want to reload some new code later, it is easier to import only a module, not names from it like "from sandbox import f", and call "sandbox.f" not "f". Then is reloading easy. (but naturarely reload command is not useful for it)

>>> class A(object): pass
... 
>>> a = A()
>>> A.f = lambda self, x: 2 * x  # or a pickled function
>>> a.f(1)
2
>>> A.f = lambda self, x: 3 * x
>>> a.f(1)
3

似乎重新加载方法可能很简单。我记得在修改后的源代码中定义的重新加载类可能会很复杂,因为旧的类代码可以由某个实例持有。在最坏的情况下,实例的代码可能需要单独更新:

It seems that reloading methods can be easy. I remember that reloading classes defined in a modified source code can be complicated because the old class code can be held by some instance. The instance's code can/need be updated individually in the worst case:

    some_instance.__class__ = sandbox.SomeClass  # that means the same reloaded class

我使用后者与通过win32com自动化访问的python服务并重新加载类代码成功没有损失实例数据

I used the latter with a python service accessed via win32com automation and reloading of classes code was succesful without loss instances data

这篇关于如何为远程沙箱执行嵌入式python模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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