进程池
""" python自带的进程池 """from multiprocessing import Poolfrom time import sleepdef 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 ThreadPoolfrom time import sleepdef 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() # 主线程等待子线程结束
posted on 2019-04-02 17:38 阅读( ...) 评论( ...)