@@ -103,7 +103,7 @@ def __init__(self, hosts,
103
103
>>> ... pass
104
104
105
105
>>> # Commands have started executing at this point
106
- >>> # Exit code will probably not be available immediately
106
+ >>> # Exit code will not be available immediately
107
107
>>> print output
108
108
109
109
::
@@ -135,10 +135,24 @@ def __init__(self, hosts,
135
135
[myhost1] drwxrwxr-x 6 user group 4.0K Jan 1 HH:MM x
136
136
[myhost2] drwxrwxr-x 6 user group 4.0K Jan 1 HH:MM x
137
137
138
- Retrieve exit codes after commands have finished as below. This is
139
- only necessary for long running commands that do not exit immediately.
138
+ Retrieve exit codes after commands have finished as below.
140
139
141
- ``exit_code`` in ``output`` will be ``None`` if command has not finished.
140
+ `parallel-ssh` starts commands asynchronously to enable running multiple
141
+ commands in parallel without blocking.
142
+
143
+ Because of this, exit codes will not be immediately available even for
144
+ commands that exit immediately.
145
+
146
+ At least one of ::
147
+
148
+ * Iterating over stdout/stderr
149
+ * Calling `client.join(output)`
150
+ * Calling `client.pool.join()` if no output is needed
151
+
152
+ is necessary to cause `parallel-ssh` to wait for commands to finish and
153
+ be able to gather exit codes.
154
+
155
+ ``exit_code`` in ``output`` will be ``None`` if command has not yet finished.
142
156
143
157
``get_exit_codes`` is not a blocking function and will not wait for commands
144
158
to finish. Use ``client.join(output)`` to block until all commands have
@@ -172,6 +186,12 @@ def __init__(self, hosts,
172
186
>>> hosts = ['dc1.myhost1', 'dc2.myhost2']
173
187
>>> client = ParallelSSHClient([h for h in hosts if h.find('dc1')])
174
188
>>> client.run_command(<..>)
189
+
190
+ **Overriding host list**
191
+
192
+ >>> client.hosts = ['otherhost']
193
+ >>> print client.run_command('exit 0')
194
+ >>> {'otherhost': {'exit_code':0}, <..>}
175
195
176
196
.. note ::
177
197
@@ -276,6 +296,34 @@ def run_command(self, *args, **kwargs):
276
296
>>> for host in output:
277
297
>>> stdout = list(output[host]['stdout'])
278
298
>>> print "Complete stdout for host %s is %s" % (host, stdout,)
299
+
300
+ **Run multiple commands in parallel**
301
+
302
+ This short example demonstrates running long running commands in parallel
303
+ and how long it takes for all commands to start, blocking until they
304
+ complete and how long it takes for all commands to complete.
305
+
306
+ See examples directory for complete example script. ::
307
+
308
+ output = []
309
+
310
+ start = datetime.datetime.now()
311
+ cmds = ['sleep 5' for _ in xrange(10)]
312
+ for cmd in cmds:
313
+ output.append(client.run_command(cmd, stop_on_errors=False))
314
+ end = datetime.datetime.now()
315
+ print "Started %s commands in %s" % (len(cmds), end-start,)
316
+ start = datetime.datetime.now()
317
+ for _output in output:
318
+ for line in _output[host]['stdout']:
319
+ print line
320
+ end = datetime.datetime.now()
321
+ print "All commands finished in %s" % (end-start,)
322
+
323
+ *Output*
324
+
325
+ Started 10 commands in 0:00:00.428629
326
+ All commands finished in 0:00:05.014757
279
327
280
328
**Example Output**
281
329
0 commit comments