我在这里找到了似乎有用的帖子:
http://eli.thegreenplace.net/2012/01/16/python-parallelizing-cpu-bound-tasks-with-multiprocessing/
我试过这段代码,它使用多处理,但对我不起作用。我对原始文件所做的唯一更改是变量 out_q=queue.Queue 而不是 out_q = Queue。
我相信这段代码是用 python 2.x 编写的,我使用的是 python 3.4.2
我当然进口了所有必需品。
def mp_factorizer(nums, nprocs):
def worker(nums, out_q):
""" The worker function, invoked in a process. 'nums' is a
list of numbers to factor. The results are placed in
a dictionary that's pushed to a queue.
"""
outdict = {}
for n in nums:
outdict[n] = factorize_naive(n)
out_q.put(outdict)
# Each process will get 'chunksize' nums and a queue to put his out
# dict into
out_q = …Run Code Online (Sandbox Code Playgroud)