from multiprocessing import Pool,Managerimport timedef hanshu(queue,a):n = 1while n50:# print(\r正在工作%d%a,end=)n+=1# 【步骤3】往队列中发送一条消息queue.put(a)time.sleep(2)def main():print(执行mai...

from multiprocessing import Pool,Manager import time def hanshu(queue,a): n = 1 while n<50: # print('\r正在工作%d'%a,end='') n+=1 # 【步骤3】往队列中发送一条消息 queue.put(a) time.sleep(2) def main(): print('执行main函数') for i in range(0,10): po.apply_async(hanshu,args=(q,i)) # 【步骤2】向进程池中添加任务,hanshu是做任务的函数,(i,)是传参,是个元组 q是队列 if __name__ == '__main__': q=Manager().Queue() #使用Manager中的Queue方法 【步骤1】创建一个队列 po = Pool(10) # 创建进程池 并限定最多同时运行10个进程 main() print('---start-----') po.close() # 关闭进程池不再接受新的进程任务 # po.join() #等待进程池里面的任务完成 while True: name=q.get() print('\r收到消息 与任务不堵塞',name ,end='')
沃梦达教程
本文标题为:Python 多进程进程池Queue进程通信


猜你喜欢
- windows安装python2.7.12和pycharm2018教程 2023-09-03
- Python 保存数据的方法(4种方法) 2023-09-04
- Python Pandas如何获取和修改任意位置的值(at,iat,loc,iloc) 2023-08-04
- CentOS7 安装 Python3.6 2023-09-04
- 在centos6.4下安装python3.5 2023-09-04
- Python之路-Python中的线程与进程 2023-09-04
- python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor 2023-09-04
- python中defaultdict用法实例详解 2022-10-20
- Python实现将DNA序列存储为tfr文件并读取流程介绍 2022-10-20
- python中列表添加元素的几种方式(+、append()、ext 2022-09-02