import multiprocessing
import time
from queue import Queue
qurl = Queue()
def func(msg):
print("msg:", msg)
time.sleep(3)
print("end")
def make_data():
for i in range(10):
qurl.put(i)
print('------ qurl.qsize() = ', qurl.qsize())
def custom():
pool = multiprocessing.Pool(processes=5)
while not qurl.empty():
data = qurl.get()
print('--- data = ', data)
pool.apply_async(func, (data,)) # 維持執行的進程總數爲processes,當一個進程執行完畢後會添加新的進程進去
pool.close()
pool.join() # 調用join之前,先調用close函數,否則會出錯。執行完close後不會有新的進程加入到pool,join函數等待所有子進程結束
if __name__ == "__main__":
start_time = time.time()
make_data()
print('make_data_end')
custom()
print('all task consum time = ', time.time() - start_time)