线程池
import threadingimport timedef myThread(): for i in range(10): time.sleep() print('d')sep=threading.Semaphore(1)threadlist=[]for name in ["a","b"]: mythd=threading.Thread(target=myThread,args=(name,)) mythd.start() threadlist.append(myThread)for thread in threadlist: thread.join()
进程池
import multiprocessingimport timeimport osdef getdata(data): print('start') time.sleep(2) print(os.getpid()) return data**dataif __name__=="__main__": mylist=[x for x in range(100)] pool=multiprocessing.Pool(processes=4) pool_outputs=pool.map(getdata,mylist) pool.close() pool.join() print(pool_outputs)
进程池
from multiprocessing import Barrierimport multiprocessingimport timeimport osdef getdata(data): with myBarrier: print('start') time.sleep(2) print(os.getpid(),data) if __name__=="__main__": myBarrier=multiprocessing.Barrier(3) mylist=[x for x in range(100)] processeslist=[] for data in mylist: pr=multiprocessing.Process(target=getdata,args(data,myBarrier)) processeslist.append(pr) pr.start() for pr in processeslist: print('n') pr.join()
协程池
import geventimport gevent.poolimport gevent.monkey gevent.monkey.patch_all()#分布式冲突def run_task(data): print(data)if __name__=="__main__": mypool=gevent.pool.Pool(2) datalist=[x for x in range(10)] result=mypool.map(run_task,datalist) print(result)
使用异步进程池,且进程池中传递入的任务是方法,
import osfrom multiprocessing import Pooldef run_process(fun): fun()def one_process(): print("执行")if __name__ == '__main__': print('parent process %s.' % os.getpid()) p = Pool() # 初始化进程池 for i in range(5): p.apply_async(run_process, args=(one_process,)) # 追加任务 apply_async 是异步非阻塞的,就是不用等待当前进程执行完毕,随时根据系统调度来进行进程切换。 p.close() p.join() # 等待所有结果执行完毕,会等待所有子进程执行完毕,调用join()之前必须先调用close()