Last active
February 10, 2017 23:00
-
-
Save yukiB/6159256a35f04c1a6354b14ffb20e551 to your computer and use it in GitHub Desktop.
Python並列処理(multiprocessingとJoblib) ref: http://qiita.com/yukiB/items/203a6248c2d466b80d38
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process(i): | |
return [{'id': j, 'sum': sum(range(i*j))} for j in range(100)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from joblib import Parallel, delayed | |
result = Parallel(n_jobs=-1)([delayed(process)(n) for n in range(1000)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from joblib import Parallel, delayed | |
from multiprocessing import Pool | |
import multiprocessing as multi | |
from more_itertools import flatten | |
import sys | |
import functools | |
def process(i): | |
return [{'id': j, 'sum': sum(range(i*j))} for j in range(1000)] | |
#def process(n): | |
# return sum([i*n for i in range(100000)]) | |
def usejoblib(job, num): | |
result =Parallel(n_jobs=job)([delayed(process)(n) for n in range(num)]) | |
return result | |
def usemulti(job, num): | |
p = Pool(multi.cpu_count() if job < 0 else job) | |
result = p.map(process, range(num)) | |
p.close() | |
return result | |
if __name__ == '__main__': | |
argv = sys.argv | |
total = 0 | |
n = 1 | |
for i in range(n): | |
s = time.time() | |
if argv[1] == 'joblib': | |
result = usejoblib(int(argv[2]),int(argv[3])) | |
elif argv[1] == 'multi': | |
result = usemulti(int(argv[2]),int(argv[3])) | |
else: | |
result = [process(j) for j in range(int(argv[3]))] | |
elapsed = time.time()-s | |
print('time: {0} [sec]'.format(elapsed)) | |
total += elapsed | |
print('--------') | |
print('average: {0} [sec]'.format(total/n)) | |
sums = functools.reduce(lambda x, y: x + y['sum'], list(flatten(result)), 0) | |
print('total: {0}'.format(sums)) | |
# print('total: {0}'.format(sum(result))) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
import multiprocessing as multi | |
p = Pool(multi.cpu_count()) | |
p.map(process, list(range(1000))) | |
p.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process(i, s): | |
return [{'id': i, 'str': s, 'sum': sum(range(i*j))} for j in range(100)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
strs = ['a', 'b', 'c'] | |
result = Parallel(n_jobs=job)([delayed(process)(i,s) for i,s in enumerate(strs * 1000)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p = Pool(multi.cpu_count()) | |
strs = ['a', 'b', 'c'] | |
p.map(process, enumerate(strs * 1000)) | |
p.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process(v): | |
return [{'id': v[0], 'str': v[1], 'sum': sum(range(v[0]*j))} for j in range(100)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process(n): | |
return sum([i*n for i in range(100000)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def usejoblib(job, num): | |
Parallel(n_jobs=job)([delayed(process)(n) for n in range(num)]) | |
def usemulti(job, num): | |
p = Pool(multi.cpu_count() if job < 0 else job) | |
p.map(process, list(range(num))) | |
p.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process(i): | |
return [{'id': j, 'sum': sum(range(i*j))} for j in range(1000)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment