1 from multiprocessing import Pool 2 import os 3 def f(x): 4 print "Os is%s"%os.getpid() 5 return x*x 6 if __name__ == ‘__main__‘: 7 pool = Pool(processes=4) 8 result = pool.apply_async(f,[10]) 9 print result.get(timeout=10) 10 print pool.map(f,range(10))
结果:
1 Os is7288 2 100 3 Os is7288 4 Os is7288 5 Os is7288 6 Os is7288 7 Os is7288 8 Os is7288 9 Os is7288 10 Os is7288 11 Os is7288 12 Os is7288 13 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
时间: 2024-10-12 04:13:50