以下代码亲测可运行,环境py3.5
案例1:使用多进程的pool+map
# coding:utf-8
import multiprocessing
def f(x):
return x * x
if __name__ == "__main__":
cores = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=cores)
xs = range(5)
# method 1: map
print(pool.map(f, xs)) # prints [0, 1, 4, 9, 16]
# method 2: imap
for y in pool.imap(f, xs):
print(y) # 0, 1, 4, 9, 16, respectively
# method 3: imap_unordered
for y in pool.imap_unordered(f, xs):
print(y) # may be in any order
cnt = 0
for _ in pool.imap_unordered(f, xs):
sys.stdout.write('done %d/%d\r' % (cnt, len(xs)))
cnt += 1
或者