Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

function - how to do an embedded python module for remote sandbox execution?

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()

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    sandbox.other_module = __import__('other_module')

or:

    exec 'import other_module' in sandbox.__dict__

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)


Classes

>>> 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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...