1. 程式人生 > >Python進程池

Python進程池

proc python blog __name__ 同時 port print close process

 1 from multiprocessing import Pool
 2 from time import sleep
 3 def Foo(i):
 4     sleep(1)
 5     print(i)
 6 
 7 
 8 if __name__ == "__main__":
 9     #5個線程會同時執行
10     pool = Pool(5)
11 
12     for i in range(50):
13         #從進程池中申請進程,還可以傳入callback參數作為進程結束後的回調函數
14         pool.apply_async(func=Foo, args=(i,))
15 pool.close() 16 pool.join()

Python進程池