spacepy.toolbox.thread_map

spacepy.toolbox.thread_map(target, iterable, thread_count=None, *args, **kwargs)[source]

Apply a function to every element of a list, in separate threads

Interface is similar to multiprocessing.map, except it runs in threads

This is made largely obsolete in python3 by from concurrent import futures

Parameters:
targetcallable
Python callable to run on each element of iterable.

For each call, an element of iterable is appended to args and both args and kwargs are passed through. Note that this means the iterable element is always the last positional argument; this allows the specification of self as the first argument for method calls.

iterableiterable

elements to pass to each call of L{target}

argssequence
arguments to pass to target before each element of

iterable

thread_countinteger

Number of threads to spawn; see L{thread_job}.

kwargsdict

keyword arguments to pass to L{target}.

Returns:
outlist

return values of L{target} for each item from L{iterable}

Examples

find totals of several arrays

>>> import numpy
>>> from spacepy import toolbox
>>> inputs = range(100)
>>> totals = toolbox.thread_map(numpy.sum, inputs)
>>> print(totals[0], totals[50], totals[99])
(0, 50, 99)
>>> # in python3
>>> from concurrent import futures
>>> with futures.ThreadPoolExecutor(max_workers=4) as executor:
...:     for ans in executor.map(numpy.sum, [0,50,99]):
...:         print ans
#0
#50
#99