立即學習:https://edu.csdn.net/course/play/24458/296451?utm_source=blogtoedu
進程池與線程池:
一般應用在網站上,進程池或線程池最大的數量一般需要盡可能地大但是不要超出服務器的承載范圍
1.進程池:
1)concurrent.futures.ProcessPoolExecutor(n)
2)創建了一個進程池,池中含有n個進程,任務都由這n個進程來完成
3)n默認為cpu的核數
4)pool.shutdown():表示關閉了新提交任務給進程池的入口,且等待進程池執行結束后再進行后面的代碼
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor#進程池與線程池
import os,random,time
def processpool(name):print('pid:%s ; %s:running'%(os.getpid(),name))time.sleep(random.randint(2,5))if __name__ == '__main__':pool = ProcessPoolExecutor(5)#創建一個進程池,池子里面有5個進程for i in range(10):pool.submit(processpool,'任務%s'%i)pool.shutdown(wait=True)print('zhu')
運行結果:由結果可知,進程的pid只有5個,10個任務一直都是由這5個進程來完成
'''
"F:\software install\python3.6.4\python.exe" C:/Users/jinlin/Desktop/python_further_study/并發編程/進程池線程池.py
pid:11848 ; 任務0:running
pid:13740 ; 任務1:running
pid:12848 ; 任務2:running
pid:7196 ; 任務3:running
pid:10884 ; 任務4:running
pid:11848 ; 任務5:running
pid:13740 ; 任務6:running
pid:12848 ; 任務7:running
pid:7196 ; 任務8:running
pid:10884 ; 任務9:running
zhu進程已結束,退出代碼0'''
2.線程池
1)concurrent.futures.ThreadPoolExecutor(n)
2)創建了一個線程池,池中含有n個線程,任務都由這n個線程來完成
3)pool.shutdown():表示關閉了新提交任務給線程池的入口,且等待線程池執行結束后再進行后面的代碼
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor#進程池與線程池
from threading import currentThread
import os,random,time
def processpool(name):print('%s pid:%s ; %s:running'%(currentThread().getName(),os.getpid(),name))time.sleep(random.randint(2,5))if __name__ == '__main__':pool = ThreadPoolExecutor(5)#創建一個進程池,池子里面有5個進程for i in range(10):pool.submit(processpool,'任務%s'%i)pool.shutdown()print('zhu')
運行結果:由結果可以知道,線程池的所有線程的pid都是一樣的,那是因為線程池中的線程都是在同一個進程中,因此pid會一致,且只有5個線程在執行任務
'''
"F:\software install\python3.6.4\python.exe" C:/Users/jinlin/Desktop/python_further_study/并發編程/進程池線程池.py
ThreadPoolExecutor-0_0 pid:11888 ; 任務0:running
ThreadPoolExecutor-0_1 pid:11888 ; 任務1:running
ThreadPoolExecutor-0_2 pid:11888 ; 任務2:running
ThreadPoolExecutor-0_3 pid:11888 ; 任務3:running
ThreadPoolExecutor-0_4 pid:11888 ; 任務4:running
ThreadPoolExecutor-0_3 pid:11888 ; 任務5:running
ThreadPoolExecutor-0_2 pid:11888 ; 任務6:running
ThreadPoolExecutor-0_1 pid:11888 ; 任務7:running
ThreadPoolExecutor-0_4 pid:11888 ; 任務8:running
ThreadPoolExecutor-0_0 pid:11888 ; 任務9:running
zhu進程已結束,退出代碼0'''
?