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: target : callable
- 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.
iterable : iterable
elements to pass to each call of L{target}
args : sequence
- arguments to pass to target before each element of
iterable
thread_count : integer
Number of threads to spawn; see L{thread_job}.
kwargs : dict
keyword arguments to pass to L{target}.
Returns: out : list
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