pushyrpc / pushy

Easy-as RPC. Zero-server RPC for Python and Java.
http://github.com/pushyrpc/pushy
45 stars 18 forks source link

Pushy lacks a simple remote "exec" method #36

Closed axw closed 12 years ago

axw commented 12 years ago

Currently to execute a non-trivial piece of code you must compile a code object and evaluate it, e.g.:- conn = pushy.connect(...) code_obj = conn.eval("compile")(source, "", "exec") conn.eval(code_obj, locals=..., globals=...)

This should be made simpler, by introducing a function that wraps these steps.


Imported from Launchpad using lp2gh.

axw commented 12 years ago

(by axwalk) I will add two new methods to pushy.client.Client: execute, and compile.

The execute method will simply be a shortcut for eval(compile(source)). e.g.:- conn.execute("print 123") = conn.eval(conn.compile("print 123"))

The compile method will do two things:

e.g.

print conn.compile("print 123") <code object at 0x94101d0, file "", line 1>

e.g. (Non-interactive; inspect.getsource only works for functions defined in files.) def sleep(sec): import time time.sleep(sec) remote_sleep = conn.compile(sleep) remote_sleep(0.5)

axw commented 12 years ago

(by axwalk) An option for compiling dynamically defined functions (e.g. in an interactive session) is to manually create a "code" object in the remote interpreter, and wrap it in a types.FunctionType. I have tested this and it works. The caveat would be that the remote Python interpreter would have to support the byte code emitted by the local one, since it would/could not be recompiled from source.

axw commented 12 years ago

(by axwalk) Done. I have added "execute" and "compile" methods as described above. The compile method will attempt first to use inspect.getsource, and, failing that, will clone the code/function object in the remote interpreter.