#! /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
#All on the elements of the list+1
print multi_plus_one(before_list)
Like this. If you just want to calculate in parallel, maybe multiprocessing.Process
#! /usr/local/bin/python
# -*- coding:utf-8
import multiprocessing
def plus_data(num, plus_num):
return num + plus_num
#Wrapper for 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)]
#All on the elements of the list+2
print multi_plus_one(before_list)
If you want to pass multiple arguments, do something like this
Recommended Posts