| 1 |
Connecting different Python interpreters |
| 2 |
========================================== |
| 3 |
|
| 4 |
.. _`dumps/loads examples`: |
| 5 |
|
| 6 |
Dumping and loading values across interpreter versions |
| 7 |
---------------------------------------------------------- |
| 8 |
|
| 9 |
.. versionadded:: 1.1 |
| 10 |
|
| 11 |
Execnet offers a new safe and fast :ref:`dumps/loads API` which you |
| 12 |
can use to dump builtin python data structures and load them |
| 13 |
later with the same or a different python interpreter (including |
| 14 |
between Python2 and Python3). The standard library offers |
| 15 |
the pickle and marshal modules but they do not work safely |
| 16 |
between different interpreter versions. Using xml/json |
| 17 |
requires a mapping of Python objects and is not easy to |
| 18 |
get right. Moreover, execnet allows to control handling |
| 19 |
of bytecode/strings/unicode types. Here is an example:: |
| 20 |
|
| 21 |
# using python2 |
| 22 |
import execnet |
| 23 |
with open("data.py23", "wb") as f: |
| 24 |
f.write(execnet.dumps(["hello", "world"])) |
| 25 |
|
| 26 |
# using Python3 |
| 27 |
import execnet |
| 28 |
with open("data.py23", "rb") as f: |
| 29 |
val = execnet.loads(f.read(), py2str_as_py3str=True) |
| 30 |
assert val == ["hello", "world"] |
| 31 |
|
| 32 |
See the :ref:`dumps/loads API` for more details on string |
| 33 |
conversion options. Please note, that you can not dump |
| 34 |
user-level instances, only builtin python types. |
| 35 |
|
| 36 |
Connect to Python2/Numpy from Python3 |
| 37 |
---------------------------------------- |
| 38 |
|
| 39 |
Here we run a Python3 interpreter to connect to a Python2.7 interpreter |
| 40 |
that has numpy installed. We send items to be added to an array and |
| 41 |
receive back the remote "repr" of the array:: |
| 42 |
|
| 43 |
import execnet |
| 44 |
gw = execnet.makegateway("popen//python=python2.7") |
| 45 |
channel = gw.remote_exec(""" |
| 46 |
import numpy |
| 47 |
array = numpy.array([1,2,3]) |
| 48 |
while 1: |
| 49 |
x = channel.receive() |
| 50 |
if x is None: |
| 51 |
break |
| 52 |
array = numpy.append(array, x) |
| 53 |
channel.send(repr(array)) |
| 54 |
""") |
| 55 |
for x in range(10): |
| 56 |
channel.send(x) |
| 57 |
channel.send(None) |
| 58 |
print (channel.receive()) |
| 59 |
|
| 60 |
will print on the CPython3.1 side:: |
| 61 |
|
| 62 |
array([1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
| 63 |
|
| 64 |
A more refined real-life example of python3/python2 interaction |
| 65 |
is the anyvc_ project which uses version-control bindings in |
| 66 |
a Python2 subprocess in order to offer Python3-based library |
| 67 |
functionality. |
| 68 |
|
| 69 |
.. _anyvc: http://bitbucket.org/RonnyPfannschmidt/anyvc/overview/ |
| 70 |
|
| 71 |
|
| 72 |
Reconfiguring the string coercion between python2 and python3 |
| 73 |
------------------------------------------------------------- |
| 74 |
|
| 75 |
Sometimes the default configuration of string coercion (2str to 3str, 3str to 2unicode) |
| 76 |
is inconvient, thus it can be reconfigured via `gw.reconfigure` and `channel.reconfigure`. Here is an example session on a Python2 interpreter:: |
| 77 |
|
| 78 |
|
| 79 |
>>> import execnet |
| 80 |
>>> execnet.makegateway("popen//python=python3.2") |
| 81 |
<Gateway id='gw0' receive-live, 0 active channels> |
| 82 |
>>> gw=execnet.makegateway("popen//python=python3.2") |
| 83 |
>>> gw.remote_exec("channel.send('hello')").receive() |
| 84 |
u'hello' |
| 85 |
>>> gw.reconfigure(py3str_as_py2str=True) |
| 86 |
>>> gw.remote_exec("channel.send('hello')").receive() |
| 87 |
'hello' |
| 88 |
>>> ch = gw.remote_exec('channel.send(type(channel.receive()).__name__)') |
| 89 |
>>> ch.send('a') |
| 90 |
>>> ch.receive() |
| 91 |
'str' |
| 92 |
>>> ch = gw.remote_exec('channel.send(type(channel.receive()).__name__)') |
| 93 |
>>> ch.reconfigure(py2str_as_py3str=False) |
| 94 |
>>> ch.send('a') |
| 95 |
>>> ch.receive() |
| 96 |
u'bytes' |
| 97 |
|
| 98 |
|
| 99 |
Work with Java objects from CPython |
| 100 |
---------------------------------------- |
| 101 |
|
| 102 |
Use your CPython interpreter to connect to a `Jython 2.5.1`_ interpreter |
| 103 |
and work with Java types:: |
| 104 |
|
| 105 |
import execnet |
| 106 |
gw = execnet.makegateway("popen//python=jython") |
| 107 |
channel = gw.remote_exec(""" |
| 108 |
from java.util import Vector |
| 109 |
v = Vector() |
| 110 |
v.add('aaa') |
| 111 |
v.add('bbb') |
| 112 |
for val in v: |
| 113 |
channel.send(val) |
| 114 |
""") |
| 115 |
|
| 116 |
for item in channel: |
| 117 |
print (item) |
| 118 |
|
| 119 |
will print on the CPython side:: |
| 120 |
|
| 121 |
aaa |
| 122 |
bbb |
| 123 |
|
| 124 |
.. _`Jython 2.5.1`: http://www.jython.org |
| 125 |
|
| 126 |
Work with C# objects from CPython |
| 127 |
---------------------------------------- |
| 128 |
|
| 129 |
(Experimental) use your CPython interpreter to connect to a IronPython_ interpreter |
| 130 |
which can work with C# classes. Here is an example for instantiating |
| 131 |
a CLR Array instance and sending back its representation:: |
| 132 |
|
| 133 |
import execnet |
| 134 |
gw = execnet.makegateway("popen//python=ipy") |
| 135 |
|
| 136 |
channel = gw.remote_exec(""" |
| 137 |
import clr |
| 138 |
clr.AddReference("System") |
| 139 |
from System import Array |
| 140 |
array = Array[float]([1,2]) |
| 141 |
channel.send(str(array)) |
| 142 |
""") |
| 143 |
print (channel.receive()) |
| 144 |
|
| 145 |
using Mono 2.0 and IronPython-1.1 this will print on the CPython side:: |
| 146 |
|
| 147 |
System.Double[](1.0, 2.0) |
| 148 |
|
| 149 |
.. note:: |
| 150 |
Using IronPython needs more testing, likely newer versions |
| 151 |
will work better. please feedback if you have information. |
| 152 |
|
| 153 |
.. _IronPython: http://ironpython.net |