Debian Codegraph @

doc/example/test_info.rst

raw
1 Basic local and remote communication
2 ====================================
3
4 Execute source code in subprocess, communicate through a channel
5 -------------------------------------------------------------------
6
7 You can instantiate a subprocess gateway, execute code
8 in it and bidirectionally send messages::
9
10 >>> import execnet
11 >>> gw = execnet.makegateway()
12 >>> channel = gw.remote_exec("channel.send(channel.receive()+1)")
13 >>> channel.send(1)
14 >>> channel.receive()
15 2
16
17 The initiating and the remote execution happen concurrently.
18 ``channel.receive()`` operations return when input is available.
19 ``channel.send(data)`` operations return when the message could
20 be delivered to the IO system.
21
22 The initiating and the "other" process work use a `share-nothing
23 model`_ and ``channel.send|receive`` are means to pass basic data
24 messages between two processes.
25
26 .. _`share-nothing model`: http://en.wikipedia.org/wiki/Shared_nothing_architecture
27
28 Remote-exec a function (avoiding inlined source part I)
29 -------------------------------------------------------
30
31 You can send and remote execute parametrized pure functions like this:
32
33 .. include:: funcmultiplier.py
34 :literal:
35
36 The ``multiplier`` function executes remotely and establishes
37 a loop multipliying incoming data with a constant factor passed
38 in via keyword arguments to ``remote_exec``.
39
40 Notes:
41
42 * unfortunately, you can not type this example interactively because
43 ``inspect.getsource(func)`` fails for interactively defined
44 functions.
45
46 * You will get an explicit error if you try to execute non-pure
47 functions, i.e. functions that access any global state (which
48 will not be available remotely as we have a share-nothing model
49 between the nodes).
50
51
52 Remote-exec a module (avoiding inlined source part II)
53 ------------------------------------------------------
54
55 You can pass a module object to ``remote_exec`` in which case
56 its source code will be sent. No dependencies will be transferred
57 so the module must be self-contained or only use modules that are
58 installed on the "other" side. Module code can detect if it is
59 running in a remote_exec situation by checking for the special
60 ``__name__`` attribute.
61
62 .. include:: remote1.py
63 :literal:
64
65 You can now remote-execute the module like this::
66
67 >>> import execnet, remote1
68 >>> gw = execnet.makegateway()
69 >>> ch = gw.remote_exec(remote1)
70 >>> print (ch.receive())
71 initialization complete
72
73 which will print the 'initialization complete' string.
74
75
76 Compare current working directories
77 ----------------------------------------
78
79 A local subprocess gateway has the same working directory as the instantiatior::
80
81 >>> import execnet, os
82 >>> gw = execnet.makegateway()
83 >>> ch = gw.remote_exec("import os; channel.send(os.getcwd())")
84 >>> res = ch.receive()
85 >>> assert res == os.getcwd()
86
87 "ssh" gateways default to the login home directory.
88
89 Get information from remote SSH account
90 ---------------------------------------
91
92 Use simple execution to obtain information from remote environments::
93
94 >>> import execnet, os
95 >>> gw = execnet.makegateway("ssh=codespeak.net")
96 >>> channel = gw.remote_exec("""
97 ... import sys, os
98 ... channel.send((sys.platform, tuple(sys.version_info), os.getpid()))
99 ... """)
100 >>> platform, version_info, remote_pid = channel.receive()
101 >>> platform
102 'linux2'
103 >>> version_info
104 (2, 6, 6, 'final', 0)
105
106 Use a callback instead of receive() and wait for completion
107 -------------------------------------------------------------
108
109 Set a channel callback to immediately react on incoming data::
110
111 >>> import execnet
112 >>> gw = execnet.makegateway()
113 >>> channel = gw.remote_exec("for i in range(10): channel.send(i)")
114 >>> l = []
115 >>> channel.setcallback(l.append, endmarker=None)
116 >>> channel.waitclose() # waits for closing, i.e. remote exec finish
117 >>> l
118 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, None]
119
120 Note that the callback function will execute in the receiver thread
121 so it should not block on IO or long to execute.
122
123 Sending channels over channels
124 ------------------------------------------------------
125
126 You can create and transfer a channel over an existing channel
127 and use it to transfer information::
128
129 >>> import execnet
130 >>> gw = execnet.makegateway()
131 >>> channel = gw.remote_exec("""
132 ... ch1, ch2 = channel.receive()
133 ... ch2.send("world")
134 ... ch1.send("hello")
135 ... """)
136 >>> c1 = gw.newchannel() # create new channel
137 >>> c2 = gw.newchannel() # create another channel
138 >>> channel.send((c1, c2)) # send them over
139 >>> c1.receive()
140 'hello'
141 >>> c2.receive()
142 'world'
143
144
145
146 A simple command loop pattern
147 --------------------------------------------------------------
148
149 If you want the remote side to serve a number
150 of synchronous function calls into your module
151 you can setup a serving loop and write a local protocol.
152
153 .. include:: remotecmd.py
154 :literal:
155
156 Then on the local side you can do::
157
158 >>> import execnet, remotecmd
159 >>> gw = execnet.makegateway()
160 >>> ch = gw.remote_exec(remotecmd)
161 >>> ch.send('simple(10)') # execute func-call remotely
162 >>> ch.receive()
163 11
164
165 Our remotecmd module starts up remote serving
166 through the ``for item in channel`` loop which
167 will terminate when the channel closes. It evaluates
168 all incoming requests in the global name space and
169 sends back the results.
170
171
172 Instantiate gateways through sockets
173 -----------------------------------------------------
174
175 .. _`socketserver.py`: https://raw.githubusercontent.com/pytest-dev/execnet/master/execnet/script/socketserver.py
176
177 In cases where you do not have SSH-access to a machine
178 you need to download a small version-independent standalone
179 `socketserver.py`_ script to provide a remote bootstrapping-point.
180 You do not need to install the execnet package remotely.
181 Simply run the script like this::
182
183 python socketserver.py :8888 # bind to all IPs, port 8888
184
185 You can then instruct execnet on your local machine to bootstrap
186 itself into the remote socket endpoint::
187
188 import execnet
189 gw = execnet.makegateway("socket=TARGET-IP:8888")
190
191 That's it, you can now use the gateway object just like
192 a popen- or SSH-based one.
193
194 .. include:: test_ssh_fileserver.rst