| 1 |
============================================================================== |
| 2 |
API in a nutshell |
| 3 |
============================================================================== |
| 4 |
|
| 5 |
execnet ad-hoc instantiates local and remote Python interpreters. |
| 6 |
Each interpreter is accessible through a **Gateway** which manages |
| 7 |
code and data communication. **Channels** allow to exchange |
| 8 |
data between the local and the remote end. **Groups** |
| 9 |
help to manage creation and termination of sub-interpreters. |
| 10 |
|
| 11 |
.. image:: _static/basic1.png |
| 12 |
|
| 13 |
.. currentmodule:: execnet |
| 14 |
|
| 15 |
Gateways: bootstrapping Python interpreters |
| 16 |
=================================================== |
| 17 |
|
| 18 |
All Gateways are instantiated via a call to ``makegateway()`` |
| 19 |
passing it a gateway specification or URL. |
| 20 |
|
| 21 |
.. _xspec: |
| 22 |
|
| 23 |
.. autofunction:: execnet.makegateway(spec) |
| 24 |
|
| 25 |
Here is an example which instantiates a simple Python subprocess:: |
| 26 |
|
| 27 |
>>> gateway = execnet.makegateway() |
| 28 |
|
| 29 |
Gateways allow to `remote execute code`_ and |
| 30 |
`exchange data`_ bidirectionally. |
| 31 |
|
| 32 |
Examples for valid gateway specifications |
| 33 |
------------------------------------------- |
| 34 |
|
| 35 |
* ``ssh=wyvern//python=python3.3//chdir=mycache`` specifies a Python3.3 |
| 36 |
interpreter on the host ``wyvern``. The remote process will have |
| 37 |
``mycache`` as its current working directory. |
| 38 |
|
| 39 |
* ``ssh=-p 5000 myhost`` makes execnet pass "-p 5000 myhost" arguments |
| 40 |
to the underlying ssh client binary, effectively specifying a custom port. |
| 41 |
|
| 42 |
* ``vagrant_ssh=default`` makes execnet connect to a Vagrant VM named |
| 43 |
``default`` via SSH through Vagrant's ``vagrant ssh`` command. It supports |
| 44 |
the same additional parameters as regular SSH connections. |
| 45 |
|
| 46 |
* ``popen//python=python2.7//nice=20`` specification of |
| 47 |
a python subprocess using the ``python2.7`` executable which must be |
| 48 |
discoverable through the system ``PATH``; running with the lowest |
| 49 |
CPU priority ("nice" level). By default current dir will be the |
| 50 |
current dir of the instantiator. |
| 51 |
|
| 52 |
* ``popen//dont_write_bytecode`` uses the same executable as the current |
| 53 |
Python, and also passes the ``-B`` flag on startup, which tells Python not |
| 54 |
write ``.pyc`` or ``.pyo`` files. |
| 55 |
|
| 56 |
* ``popen//env:NAME=value`` specifies a subprocess that uses the |
| 57 |
same interpreter as the one it is initiated from and additionally |
| 58 |
remotely sets an environment variable ``NAME`` to ``value``. |
| 59 |
|
| 60 |
* ``popen//execmodel=eventlet`` specifies a subprocess that uses the |
| 61 |
same interpreter as the one it is initiated from but will run the |
| 62 |
other side using eventlet for handling IO and dispatching threads. |
| 63 |
|
| 64 |
* ``socket=192.168.1.4:8888`` specifies a Python Socket server |
| 65 |
process that listens on ``192.168.1.4:8888`` |
| 66 |
|
| 67 |
.. versionadded:: 1.5 |
| 68 |
|
| 69 |
* ``vagarant_ssh`` opens a python interpreter via the vagarant ssh command |
| 70 |
|
| 71 |
|
| 72 |
.. _`remote execute code`: |
| 73 |
|
| 74 |
remote_exec: execute source code remotely |
| 75 |
=================================================== |
| 76 |
|
| 77 |
.. currentmodule:: execnet.gateway |
| 78 |
|
| 79 |
All gateways offer a simple method to execute source code |
| 80 |
in the instantiated subprocess-interpreter: |
| 81 |
|
| 82 |
.. automethod:: Gateway.remote_exec(source) |
| 83 |
|
| 84 |
It is allowed to pass a module object as source code |
| 85 |
in which case its source code will be obtained and |
| 86 |
get sent for remote execution. ``remote_exec`` returns |
| 87 |
a channel object whose symmetric counterpart channel |
| 88 |
is available to the remotely executing source. |
| 89 |
|
| 90 |
|
| 91 |
.. method:: Gateway.reconfigure([py2str_as_py3str=True, py3str_as_py2str=False]) |
| 92 |
|
| 93 |
Reconfigures the string-coercion behaviour of the gateway |
| 94 |
|
| 95 |
.. _`Channel`: |
| 96 |
.. _`channel-api`: |
| 97 |
|
| 98 |
.. _`exchange data`: |
| 99 |
|
| 100 |
Channels: exchanging data with remote code |
| 101 |
======================================================= |
| 102 |
|
| 103 |
.. currentmodule:: execnet.gateway_base |
| 104 |
|
| 105 |
A channel object allows to send and receive data between |
| 106 |
two asynchronously running programs. |
| 107 |
|
| 108 |
.. automethod:: Channel.send(item) |
| 109 |
.. automethod:: Channel.receive(timeout) |
| 110 |
.. automethod:: Channel.setcallback(callback, endmarker=_NOENDMARKER) |
| 111 |
.. automethod:: Channel.makefile(mode, proxyclose=False) |
| 112 |
.. automethod:: Channel.close(error) |
| 113 |
.. automethod:: Channel.waitclose(timeout) |
| 114 |
.. autoattribute:: Channel.RemoteError |
| 115 |
.. autoattribute:: Channel.TimeoutError |
| 116 |
|
| 117 |
|
| 118 |
.. _Group: |
| 119 |
|
| 120 |
Grouped Gateways and robust termination |
| 121 |
=============================================== |
| 122 |
|
| 123 |
.. currentmodule:: execnet.multi |
| 124 |
|
| 125 |
All created gateway instances are part of a group. If you |
| 126 |
call ``execnet.makegateway`` it actually is forwarded to |
| 127 |
the ``execnet.default_group``. Group objects are container |
| 128 |
objects (see :doc:`group examples <example/test_group>`) |
| 129 |
and manage the final termination procedure: |
| 130 |
|
| 131 |
.. automethod:: Group.terminate(timeout=None) |
| 132 |
|
| 133 |
This method is implicitly called for each gateway group at |
| 134 |
process-exit, using a small timeout. This is fine |
| 135 |
for interactive sessions or random scripts which |
| 136 |
you rather like to error out than hang. If you start many |
| 137 |
processes then you often want to call ``group.terminate()`` |
| 138 |
yourself and specify a larger or not timeout. |
| 139 |
|
| 140 |
|
| 141 |
threading models: gevent, eventlet, thread, main_thread_only |
| 142 |
==================================================================== |
| 143 |
|
| 144 |
.. versionadded:: 1.2 (status: experimental!) |
| 145 |
|
| 146 |
execnet supports "main_thread_only", "thread", "eventlet" and "gevent" |
| 147 |
as thread models on each of the two sides. You need to decide which |
| 148 |
model to use before you create any gateways:: |
| 149 |
|
| 150 |
# content of threadmodel.py |
| 151 |
import execnet |
| 152 |
# locally use "eventlet", remotely use "thread" model |
| 153 |
execnet.set_execmodel("eventlet", "thread") |
| 154 |
gw = execnet.makegateway() |
| 155 |
print (gw) |
| 156 |
print (gw.remote_status()) |
| 157 |
print (gw.remote_exec("channel.send(1)").receive()) |
| 158 |
|
| 159 |
You need to have eventlet installed in your environment and then |
| 160 |
you can execute this little test file:: |
| 161 |
|
| 162 |
$ python threadmodel.py |
| 163 |
<Gateway id='gw0' receive-live, eventlet model, 0 active channels> |
| 164 |
<RInfo 'numchannels=0, numexecuting=0, execmodel=thread'> |
| 165 |
1 |
| 166 |
|
| 167 |
How to execute in the main thread |
| 168 |
------------------------------------------------ |
| 169 |
|
| 170 |
When the remote side of a gateway uses the "thread" model, execution |
| 171 |
will preferably run in the main thread. This allows GUI loops |
| 172 |
or other code to behave correctly. If you, however, start multiple |
| 173 |
executions concurrently, they will run in non-main threads. |
| 174 |
|
| 175 |
|
| 176 |
remote_status: get low-level execution info |
| 177 |
=================================================== |
| 178 |
|
| 179 |
.. currentmodule:: execnet.gateway |
| 180 |
|
| 181 |
All gateways offer a simple method to obtain some status |
| 182 |
information from the remote side. |
| 183 |
|
| 184 |
.. automethod:: Gateway.remote_status(source) |
| 185 |
|
| 186 |
Calling this method tells you e.g. how many execution |
| 187 |
tasks are queued, how many are executing and how many |
| 188 |
channels are active. |
| 189 |
|
| 190 |
rsync: synchronise filesystem with remote |
| 191 |
=============================================================== |
| 192 |
|
| 193 |
.. currentmodule:: execnet |
| 194 |
|
| 195 |
|
| 196 |
``execnet`` implements a simple efficient rsyncing protocol. |
| 197 |
Here is a basic example for using RSync:: |
| 198 |
|
| 199 |
rsync = execnet.RSync('/tmp/source') |
| 200 |
gw = execnet.makegateway() |
| 201 |
rsync.add_target(gw, '/tmp/dest') |
| 202 |
rsync.send() |
| 203 |
|
| 204 |
|
| 205 |
And here is API info about the RSync class. |
| 206 |
|
| 207 |
.. autoclass:: RSync |
| 208 |
:members: add_target,send |
| 209 |
|
| 210 |
Debugging execnet |
| 211 |
=============================================================== |
| 212 |
|
| 213 |
By setting the environment variable ``EXECNET_DEBUG`` you can |
| 214 |
configure a tracing mechanism: |
| 215 |
|
| 216 |
:EXECNET_DEBUG=1: write per-process trace-files to ``execnet-debug-PID`` |
| 217 |
:EXECNET_DEBUG=2: perform tracing to stderr (popen-gateway workers will send this to their instantiator) |
| 218 |
|
| 219 |
|
| 220 |
.. _`dumps/loads`: |
| 221 |
.. _`dumps/loads API`: |
| 222 |
|
| 223 |
Cross-interpreter serialization of Python objects |
| 224 |
======================================================= |
| 225 |
|
| 226 |
.. versionadded:: 1.1 |
| 227 |
|
| 228 |
Execnet exposes a function pair which you can safely use to |
| 229 |
store and load values from different Python interpreters |
| 230 |
(e.g. Python2 and Python3, PyPy and Jython). Here is |
| 231 |
a basic example:: |
| 232 |
|
| 233 |
>>> import execnet |
| 234 |
>>> dump = execnet.dumps([1,2,3]) |
| 235 |
>>> execnet.loads(dump) |
| 236 |
[1,2,3] |
| 237 |
|
| 238 |
For more examples see :ref:`dumps/loads examples`. |
| 239 |
|
| 240 |
.. autofunction:: execnet.dumps(spec) |
| 241 |
.. autofunction:: execnet.loads(spec) |