concurrent.futures问题:为什么只有1名工人?

时间:2017-02-05 06:05:33

标签: python python-3.x concurrency multiprocessing concurrent.futures

我正在尝试使用concurrent.futures.ProcessPoolExecutor来并行化串行任务。串行任务涉及从数字范围中查找给定数字的出现次数。我的代码如下所示。
在执行期间,我注意到在任务管理器/系统监视器/顶部,尽管给processPoolExecutor的max_workers值大于1,但只有一个cpu /线程一直在运行。为什么会这样?如何使用concurrent.futures?并行化我的代码我的代码是用python 3.5执行的。

import concurrent.futures as cf
from time import time

def _findmatch(nmax, number):    
    print('def _findmatch(nmax, number):')
    start = time()
    match=[]
    nlist = range(nmax)
    for n in nlist:
        if number in str(n):match.append(n)
    end = time() - start
    print("found {} in {}sec".format(len(match),end))
    return match

def _concurrent(nmax, number, workers):
    with cf.ProcessPoolExecutor(max_workers=workers) as executor:
        start = time()
        future = executor.submit(_findmatch, nmax, number)
        futures = future.result()
        found = len(futures)
        end = time() - start
        print('with statement of def _concurrent(nmax, number):')
        print("found {} in {}sec".format(found, end))
    return futures

if __name__ == '__main__':
    match=[]
    nmax = int(1E8)
    number = str(5) # Find this number
    workers = 3
    start = time()
    a = _concurrent(nmax, number, workers)
    end = time() - start
    print('main')
    print("found {} in {}sec".format(len(a),end))

2 个答案:

答案 0 :(得分:1)

运行代码显示所有三名工作人员都在那里,但其中两人正在睡觉。问题是,executor.submit(_findmatch, nmax, number)只告诉一个工作人员执行函数_findmatch

我不明白你的代码在做什么,但基本上你需要

  • 将任务分成三个偶数部分,并使用executor.submit
  • 将每个部分发送到流程
  • 将任务拆分为较小的块(假设一个块包含所有100个元素)并使用map,这样每个_findmatch只获得它所分配的块。

答案 1 :(得分:1)

您的代码的问题在于它只提交一个任务,然后由其中一个工作人员执行,而其余工作人员什么都不做。您需要提交可由工作人员并行执行的多个任务。

下面的示例将搜索区域拆分为三个不同的任务,每个任务由不同的工作人员执行。 submit返回的期货会被添加到列表中,一旦提交了所有期货,wait将用于等待所有期货完成。如果您在提交任务后立即致电result,它将会阻止,直到将来完成。

请注意,下面的代码不是生成数字列表,而是只计算其中包含数字5的数字,以减少内存使用量:

import concurrent.futures as cf
from time import time

def _findmatch(nmin, nmax, number):
    print('def _findmatch', nmin, nmax, number)
    start = time()
    count = 0
    for n in range(nmin, nmax):
        if number in str(n):
            count += 1
    end = time() - start
    print("found {} in {}sec".format(count,end))
    return count

def _concurrent(nmax, number, workers):
    with cf.ProcessPoolExecutor(max_workers=workers) as executor:
        start = time()
        chunk = nmax // workers
        futures = []

        for i in range(workers):
            cstart = chunk * i
            cstop = chunk * (i + 1) if i != workers - 1 else nmax

            futures.append(executor.submit(_findmatch, cstart, cstop, number))

        cf.wait(futures)
        res = sum(f.result() for f in futures)
        end = time() - start
        print('with statement of def _concurrent(nmax, number):')
        print("found {} in {}sec".format(res, end))
    return res

if __name__ == '__main__':
    match=[]
    nmax = int(1E8)
    number = str(5) # Find this number
    workers = 3
    start = time()
    a = _concurrent(nmax, number, workers)
    end = time() - start
    print('main')
    print("found {} in {}sec".format(a,end))

输出:

def _findmatch 0 33333333 5
def _findmatch 33333333 66666666 5
def _findmatch 66666666 100000000 5
found 17190813 in 20.09431290626526sec
found 17190813 in 20.443560361862183sec
found 22571653 in 20.47660517692566sec
with statement of def _concurrent(nmax, number):
found 56953279 in 20.6196870803833sec
main
found 56953279 in 20.648695707321167sec