In non-blocking mode, apply() submits the command to be executed and then returns a AsyncResult object immediately. The AsyncResult object gives you a way of getting a result at a later time through its get() method, but it also collects metadata on execution.
Our AsyncResult objects add a number of convenient features for working with parallel results, beyond what is provided by the original AsyncResult.
First, is AsyncResult.get_dict(), which pulls results as a dictionary keyed by engine_id, rather than a flat list. This is useful for quickly coordinating or distributing information about all of the engines.
As an example, here is a quick call that gives every engine a dict showing the PID of every other engine:
In : ar = rc[:].apply_async(os.getpid) In : pids = ar.get_dict() In : rc[:]['pid_map'] = pids
This trick is particularly useful when setting up inter-engine communication, as in IPython’s examples/parallel/interengine examples.
IPython.parallel tracks some metadata about the tasks, which is stored in the Client.metadata dict. The AsyncResult object gives you an interface for this information as well, including timestamps stdout/err, and engine IDs.
IPython tracks various timestamps as datetime objects, and the AsyncResult object has a few properties that turn these into useful times (in seconds as floats).
For use while the tasks are still pending:
ar.elapsed is just the elapsed seconds since submission, for use before the AsyncResult is complete.
ar.progress is the number of tasks that have completed. Fractional progress would be:
1.0 * ar.progress / len(ar)
AsyncResult.wait_interactive() will wait for the result to finish, but print out status updates on progress and elapsed time while it waits.
For use after the tasks are done:
wall_time is only precise if the Client is waiting for results when the task finished, because the received timestamp is made when the result is unpacked by the Client, triggered by the spin() call. If you are doing work in the Client, and not waiting/spinning, then received might be artificially high.
An often interesting metric is the time it actually cost to do the work in parallel relative to the serial computation, and this can be given simply with
speedup = ar.serial_time / ar.wall_time
When an AsyncResult object has multiple results (e.g. the AsyncMapResult object), you can actually iterate through results themselves, and act on them as they arrive:
That is to say, if you treat an AsyncMapResult as if it were a list of your actual results, it should behave as you would expect, with the only difference being that you can start iterating through the results before they have even been computed.
This lets you do a dumb version of map/reduce with the builtin Python functions, and the only difference between doing this locally and doing it remotely in parallel is using the asynchronous view.map instead of the builtin map.
Here is a simple one-line RMS (root-mean-square) implemented with Python’s builtin map/reduce.
In : X = np.linspace(0,100) In : from math import sqrt In : add = lambda a,b: a+b In : sq = lambda x: x*x In : sqrt(reduce(add, map(sq, X)) / len(X)) Out: 58.028845747399714 In : sqrt(reduce(add, view.map(sq, X)) / len(X)) Out: 58.028845747399714
To break that down:
When AsyncResult or the AsyncMapResult don’t provide what you need (for instance, handling individual results as they arrive, but with metadata), you can always just split the original result’s msg_ids attribute, and handle them as you like.
For an example of this, see examples/parallel/customresult.py