python中多进程multiprocessing、多线程threading、线程池threadpool

浅显点理解:进程就是一个程序,里面的线程就是用来干活的,,,进程大,线程小

python中多进程multiprocessing、多线程threading、线程池threadpool

一、多线程threading

简单的单线程和多线程运行:一个参数时,后面要加逗号

步骤:for循环,相当于多个线程——t=threading.Thread(target=函数名,args=(参数,))——t.start()——while threading.active_count()!=1:pass

python中多进程multiprocessing、多线程threading、线程池threadpool

 添加主线程的打印功能,会发现,会先运行完主线程,才会去运行子线程

python中多进程multiprocessing、多线程threading、线程池threadpool

要想先运行完子线程再去运行主线程的2个办法:

python中多进程multiprocessing、多线程threading、线程池threadpool

举例:多线程下载图片

python中多进程multiprocessing、多线程threading、线程池threadpool

二、多进程multiprocessing:windows一定要放在if __name__=='__mian__'下面运行,否则会报错

步骤:for循环,相当于创建多个进程——p=multiprocessing.Process(target=函数名,args=(参数,))——p.start()——while len(multiprocessing.active_children())!=1:  pass

python中多进程multiprocessing、多线程threading、线程池threadpool

三、线程池 threadpool

步骤:pool=threadpool.ThreadPool(200)——reqs=threadpool.makeRequests(函数名,数据)——[pool.putRequest(t) for t in reqs]——pool.wait()

python中多进程multiprocessing、多线程threading、线程池threadpool