进程池
""" python自带的进程池 """ from multiprocessing import Pool from time import sleep def apply(*args, **kwargs): print(args, kwargs) sleep(3) return 'ok' if __name__ == '__main__': pool = Pool(3) result = pool.apply_async(apply, args=(1,2), kwds={'a':1}) print(result.get()) # 'ok' , 池的值操作, 也会被进程阻塞3s pool.close() # 停止往进程池添加任务 pool.join() # 主线程等待进程程结束
线程池
""" python自带的线程池 """ from multiprocessing.pool import ThreadPool from time import sleep def apply(*args, **kwargs): print(args, kwargs) sleep(3) pool = ThreadPool(3) pool.apply_async(apply, args=(1,2), kwds={'a':1}) pool.close() # 停止往线程池添加任务 pool.join() # 主线程等待子线程结束