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

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

 

1、多线程threading多线程

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

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

 

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

 

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

 

举例:多线程下载图片图片

 

 

2、多进程multiprocessing:windows必定要放在if __name__=='__mian__'下面运行,不然会报错ip

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

 

3、线程池 threadpoolit

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

相关文章
相关标签/搜索