def map(self, func, iterable, chunksize=None, callback=None):
"""
Equivalent to the built-in ``map()`` function and
:meth:`multiprocessing.pool.Pool.map()`, without catching
``KeyboardInterrupt``.
Parameters
----------
worker : callable
A function or callable object that is executed on each element of
the specified ``tasks`` iterable. This object must be picklable
(i.e. it can't be a function scoped within a function or a
``lambda`` function). This should accept a single positional
argument and return a single object.
tasks : iterable
A list or iterable of tasks. Each task can be itself an iterable
(e.g., tuple) of values or data to pass in to the worker function.
callback : callable, optional
An optional callback function (or callable) that is called with the
result from each worker run and is executed on the master process.
This is useful for, e.g., saving results to a file, since the
callback is only called on the master thread.
Returns
-------
results : list
A list of results from the output of each ``worker()`` call.
"""
if callback is None:
callbackwrapper = None
else:
callbackwrapper = CallbackWrapper(callback)
# The key magic is that we must call r.get() with a timeout, because
# a Condition.wait() without a timeout swallows KeyboardInterrupts.
r = self.map_async(func, iterable, chunksize=chunksize,
callback=callbackwrapper)
while True:
try:
return r.get(self.wait_timeout)
except multiprocessing.TimeoutError:
pass
except KeyboardInterrupt:
self.terminate()
self.join()
raise
评论列表
文章目录