您的代码的问题在于,它仅提交一项任务,然后由其中一名工作人员执行该任务,而其余工作人员则不执行任何操作。您需要提交多个可以由工作人员并行执行的任务。
下面的示例将搜索区域分为三个不同的任务,每个任务由不同的工作人员执行。期货返还方式submit https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.submit被添加到列表中,一旦全部提交wait https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.wait用于等待它们全部完成。如果你打电话result https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future.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))
Output:
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