网站建站如何入手,一级造价工程师成绩查询,百度app广告,公司网站设计要求立即学习:https://edu.csdn.net/course/play/24458/296451?utm_sourceblogtoedu
进程池与线程池#xff1a;
一般应用在网站上#xff0c;进程池或线程池最大的数量一般需要尽可能地大但是不要超出服务器的承载范围
1.进程池#xff1a;
1)concurrent.futures.ProcessP…立即学习:https://edu.csdn.net/course/play/24458/296451?utm_sourceblogtoedu
进程池与线程池
一般应用在网站上进程池或线程池最大的数量一般需要尽可能地大但是不要超出服务器的承载范围
1.进程池
1)concurrent.futures.ProcessPoolExecutor(n)
2)创建了一个进程池池中含有n个进程任务都由这n个进程来完成
3)n默认为cpu的核数
4pool.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(waitTrue)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进程已结束,退出代码02.线程池
1)concurrent.futures.ThreadPoolExecutor(n)
2)创建了一个线程池池中含有n个线程任务都由这n个线程来完成
3pool.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