Edited at

multiprocessingで並列処理

More than 5 years have passed since last update.

#! /usr/local/bin/python

# -*- coding:utf-8

import multiprocessing

def plus_data(num):
return num+1

def multi_plus_one(before_list):
pool = multiprocessing.Pool(processes=4)
return pool.map(plus_data, before_list)

if __name__ == "__main__":
before_list = range(10)
print before_list
# リストの要素にすべて+1
print multi_plus_one(before_list)

こんな感じ。ただ並列計算したいだけならmultiprocessing.Processかな

#! /usr/local/bin/python

# -*- coding:utf-8

import multiprocessing

def plus_data(num, plus_num):
return num + plus_num

# multi pool用のラッパー
def wrapper_plus_data(args):
return plus_data(*args)

def multi_plus_one(before_list):
pool = multiprocessing.Pool(processes=4)
return pool.map(wrapper_plus_data, before_list)

if __name__ == "__main__":
before_list = [(i, 2) for i in range(10)]
# リストの要素にすべて+2
print multi_plus_one(before_list)

複数の引数を渡したいときはこんなかんじにすれば