Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python / multiprocessing test in case a function has multi arguments.
from multiprocessing import Pool
import os
import time
start = time.time()
def func(n, a1, a2):
time.sleep(0.1)
r = n * a1 + a2
print('time:{:1.16f} r:{:5d} pid:{}'.format(time.time() - start, r, os.getpid()))
return r
def wrapper(args):
return func(*args)
if __name__ == '__main__':
process = 3
lst = [(i, 2, 5) for i in range(10)]
p = Pool(process)
output = p.map(wrapper, lst)
print(process)
print(time.time() - start)
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment