9 def _init(current
, total
):
16 def _wrapped_func(func_and_args
):
17 func
, argument
, should_print_progress
, filter_
= func_and_args
19 if should_print_progress
:
20 with _current
.get_lock():
22 sys
.stdout
.write('\r\t{} of {}'.format(_current
.value
, _total
.value
))
25 return func(argument
, filter_
)
28 def pmap(func
, iterable
, processes
, should_print_progress
, filter_
=None, *args
, **kwargs
):
30 A parallel map function that reports on its progress.
32 Applies `func` to every item of `iterable` and return a list of the
33 results. If `processes` is greater than one, a process pool is used to run
34 the functions in parallel. `should_print_progress` is a boolean value that
35 indicates whether a string 'N of M' should be printed to indicate how many
36 of the functions have finished being run.
40 _current
= multiprocessing
.Value('i', 0)
41 _total
= multiprocessing
.Value('i', len(iterable
))
43 func_and_args
= [(func
, arg
, should_print_progress
, filter_
) for arg
in iterable
]
45 result
= list(map(_wrapped_func
, func_and_args
, *args
, **kwargs
))
47 pool
= multiprocessing
.Pool(initializer
=_init
,
48 initargs
=(_current
, _total
,),
50 result
= pool
.map(_wrapped_func
, func_and_args
, *args
, **kwargs
)
54 if should_print_progress
:
55 sys
.stdout
.write('\r')