我正在寻找一个简单的例子,清楚地显示单个任务如何被分配到多进程。
说实话,许多例子过于复杂,使得流程更难操作。
是否有人愿意分享他们的突破样本或示例?
我正在寻找一个简单的例子,清楚地显示单个任务如何被分配到多进程。
说实话,许多例子过于复杂,使得流程更难操作。
是否有人愿意分享他们的突破样本或示例?
你的基本示例是这样的:
>>> import multiprocessing as mp
>>> from math import sqrt
>>> worker_pool = mp.Pool()
>>> jobs = [0, 1, 4, 9, 16, 25]
>>>
>>> # calculate jobs in blocking batch parallel
>>> results = worker_pool.map(sqrt, jobs)
>>> results
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
>>>
>>> # calculate jobs in asynchronous parallel
>>> results = worker_pool.map_async(sqrt, jobs)
>>> results.get()
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
>>>
>>> # calculate jobs in parallel with an unordered iterator
>>> results = worker_pool.imap_unordered(sqrt, jobs)
>>> list(results) # NOTE: results may return out of order
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
>>>
>>> # a single blocking job on another process
>>> worker_pool.apply(sqrt, [9])
3.0
>>> # a single asynchronous job on another process
>>> y = worker_pool.apply_async(sqrt, [9])
>>> y.get()
3.0
>>> # the same interface exists for threads
>>> thread_pool = mp.dummy.Pool()
>>> thread_pool.map(sqrt, jobs)
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
>>>
>>> # finishing up, you should shut down your pools
>>> worker_pool.close()
>>> worker_pool.join()
>>> thread_pool.close()
>>> thread_pool.join()
如果您不想进行批量并行处理,而是需要更复杂的操作,则示例可能会变得更加复杂。