我想加快在python中填充数组。我想知道如何使用并行处理。我的代码如下所示:
def get_value(val):
##THE RESPONSE OF THE REQUEST TAKES SOME TIME##
return subprocess.check_output('curl -d "data='+i+'" http://my/url,com',shell=True)
N = 1000000
output = [0]*N
for i in range(N):
output[i] = get_value(i)
在这种情况下,如何并行填充输出数组呢?
答案 0 :(得分:1)
试试这个!
import multiprocessing
import subprocess
def get_value(val):
##THE RESPONSE OF THE REQUEST TAKES SOME TIME##
return subprocess.check_output('curl -d "data='+ str(val)+'" http://my/url,com',shell=True)
N = 1000000
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
outputs = pool.map(get_value, range(N))
pool.close()