Python自定义进程池实例分析【生产者、消费者模型问题】


Posted in Python onSeptember 19, 2016

本文实例分析了Python自定义进程池。分享给大家供大家参考,具体如下:

代码说明一切:

#encoding=utf-8
#author: walker
#date: 2014-05-21
#function: 自定义进程池遍历目录下文件
from multiprocessing import Process, Queue, Lock
import time, os
#消费者
class Consumer(Process):
  def __init__(self, queue, ioLock):
    super(Consumer, self).__init__()
    self.queue = queue
    self.ioLock = ioLock
  def run(self):
    while True:
      task = self.queue.get()  #队列中无任务时,会阻塞进程
      if isinstance(task, str) and task == 'quit':
        break;
      time.sleep(1)  #假定任务处理需要1秒钟
      self.ioLock.acquire()
      print( str(os.getpid()) + ' ' + task)
      self.ioLock.release()
    self.ioLock.acquire()
    print 'Bye-bye'
    self.ioLock.release()
#生产者
def Producer():
  queue = Queue()  #这个队列是进程/线程安全的
  ioLock = Lock()
  subNum = 4  #子进程数量
  workers = build_worker_pool(queue, ioLock, subNum)
  start_time = time.time()
  for parent, dirnames, filenames in os.walk(r'D:\test'):
    for filename in filenames:
      queue.put(filename)
      ioLock.acquire()
      print('qsize:' + str(queue.qsize()))
      ioLock.release()
      while queue.qsize() > subNum * 10: #控制队列中任务数量
        time.sleep(1)
  for worker in workers:
    queue.put('quit')
  for worker in workers:
    worker.join()
  ioLock.acquire()
  print('Done! Time taken: {}'.format(time.time() - start_time))
  ioLock.release()
#创建进程池
def build_worker_pool(queue, ioLock, size):
  workers = []
  for _ in range(size):
    worker = Consumer(queue, ioLock)
    worker.start()
    workers.append(worker)
  return workers
if __name__ == '__main__':
  Producer()

ps:

self.ioLock.acquire()
...
self.ioLock.release()

可用:

with self.ioLock:
  ...

替代。

再来一个好玩的例子:

#encoding=utf-8
#author: walker
#date: 2016-01-06
#function: 一个多进程的好玩例子
import os, sys, time
from multiprocessing import Pool
cur_dir_fullpath = os.path.dirname(os.path.abspath(__file__))
g_List = ['a']
#修改全局变量g_List
def ModifyDict_1():
  global g_List
  g_List.append('b')
#修改全局变量g_List
def ModifyDict_2():
  global g_List
  g_List.append('c')
#处理一个
def ProcOne(num):
  print('ProcOne ' + str(num) + ', g_List:' + repr(g_List))
#处理所有
def ProcAll():
  pool = Pool(processes = 4)
  for i in range(1, 20):
    #ProcOne(i)
    #pool.apply(ProcOne, (i,))
    pool.apply_async(ProcOne, (i,))
  pool.close()
  pool.join()
ModifyDict_1() #修改全局变量g_List
if __name__ == '__main__':
  ModifyDict_2() #修改全局变量g_List
  print('In main g_List :' + repr(g_List))
  ProcAll()

Windows7 下运行的结果:

λ python3 demo.py
In main g_List :['a', 'b', 'c']
ProcOne 1, g_List:['a', 'b']
ProcOne 2, g_List:['a', 'b']
ProcOne 3, g_List:['a', 'b']
ProcOne 4, g_List:['a', 'b']
ProcOne 5, g_List:['a', 'b']
ProcOne 6, g_List:['a', 'b']
ProcOne 7, g_List:['a', 'b']
ProcOne 8, g_List:['a', 'b']
ProcOne 9, g_List:['a', 'b']
ProcOne 10, g_List:['a', 'b']
ProcOne 11, g_List:['a', 'b']
ProcOne 12, g_List:['a', 'b']
ProcOne 13, g_List:['a', 'b']
ProcOne 14, g_List:['a', 'b']
ProcOne 15, g_List:['a', 'b']
ProcOne 16, g_List:['a', 'b']
ProcOne 17, g_List:['a', 'b']
ProcOne 18, g_List:['a', 'b']
ProcOne 19, g_List:['a', 'b']

Ubuntu 14.04下运行的结果:

In main g_List :['a', 'b', 'c']
ProcOne 1, g_List:['a', 'b', 'c']
ProcOne 2, g_List:['a', 'b', 'c']
ProcOne 3, g_List:['a', 'b', 'c']
ProcOne 5, g_List:['a', 'b', 'c']
ProcOne 4, g_List:['a', 'b', 'c']
ProcOne 8, g_List:['a', 'b', 'c']
ProcOne 9, g_List:['a', 'b', 'c']
ProcOne 7, g_List:['a', 'b', 'c']
ProcOne 11, g_List:['a', 'b', 'c']
ProcOne 6, g_List:['a', 'b', 'c']
ProcOne 12, g_List:['a', 'b', 'c']
ProcOne 13, g_List:['a', 'b', 'c']
ProcOne 10, g_List:['a', 'b', 'c']
ProcOne 14, g_List:['a', 'b', 'c']
ProcOne 15, g_List:['a', 'b', 'c']
ProcOne 16, g_List:['a', 'b', 'c']
ProcOne 17, g_List:['a', 'b', 'c']
ProcOne 18, g_List:['a', 'b', 'c']
ProcOne 19, g_List:['a', 'b', 'c']

可以看见Windows7下第二次修改没有成功,而Ubuntu下修改成功了。据uliweb作者limodou讲,原因是Windows下是充重启实现的子进程;Linux下是fork实现的。

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
替换python字典中的key值方法
Jul 06 Python
python pyheatmap包绘制热力图
Nov 09 Python
Python3 使用cookiejar管理cookie的方法
Dec 28 Python
python使用selenium实现批量文件下载
Mar 11 Python
Pytorch to(device)用法
Jan 08 Python
在Python中使用K-Means聚类和PCA主成分分析进行图像压缩
Apr 10 Python
基于python实现图片转字符画代码实例
Sep 04 Python
python实现简单的tcp 文件下载
Sep 16 Python
selenium自动化测试入门实战
Dec 21 Python
解决Pycharm 运行后没有输出的问题
Feb 05 Python
python实现ROA算子边缘检测算法
Apr 05 Python
python实现MD5进行文件去重的示例代码
Jul 09 Python
python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法
Sep 19 #Python
Python操作Access数据库基本步骤分析
Sep 19 #Python
Python自定义主从分布式架构实例分析
Sep 19 #Python
Python实现网络端口转发和重定向的方法
Sep 19 #Python
Python中__init__.py文件的作用详解
Sep 18 #Python
Python简单遍历字典及删除元素的方法
Sep 18 #Python
Python实现压缩与解压gzip大文件的方法
Sep 18 #Python
You might like
用php来检测proxy
2006/10/09 PHP
通过php修改xml文档内容的方法
2015/01/23 PHP
[原创]php简单防盗链验证实现方法
2016/07/09 PHP
php执行多个存储过程的方法【基于thinkPHP】
2016/11/08 PHP
简单谈谈 php 文件锁
2017/02/19 PHP
PHP赋值的内部是如何跑的详解
2019/01/13 PHP
jQuery源码分析-01总体架构分析
2011/11/14 Javascript
jQuery制作圣诞主题页面 更像是爱情影集
2016/08/10 Javascript
jQuery视差滚动效果网页实现方法经验总结
2016/09/29 Javascript
JavaScript中捕获/阻止捕获、冒泡/阻止冒泡方法
2016/12/07 Javascript
JS回调函数基本定义与用法实例分析
2017/05/24 Javascript
不使用 JS 匿名函数理由
2017/11/17 Javascript
jQuery实现弹窗下底部页面禁止滑动效果
2017/12/19 jQuery
基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果
2018/01/09 Javascript
微信小程序登录态和检验注册过没的app.js写法
2019/05/22 Javascript
从表单校验看JavaScript策略模式的使用详解
2020/10/17 Javascript
Python合并多个装饰器小技巧
2015/04/28 Python
基于进程内通讯的python聊天室实现方法
2015/06/28 Python
Jupyter安装nbextensions,启动提示没有nbextensions库
2020/04/23 Python
使用pandas对矢量化数据进行替换处理的方法
2018/04/11 Python
Django项目使用CircleCI的方法示例
2019/07/14 Python
Python selenium 加载并保存QQ群成员,去除其群主、管理员信息的示例代码
2020/05/28 Python
python调用jenkinsAPI构建jenkins,并传递参数的示例
2020/12/09 Python
移动Web—CSS为Retina屏幕替换更高质量的图片
2012/12/24 HTML / CSS
瑞典首都斯德哥尔摩的多元奢侈时尚品牌:Acne Studios
2017/07/09 全球购物
绿色美容,有机护肤品和化妆品:Safe & Chic
2018/10/29 全球购物
高级运动鞋:GREATS
2019/07/19 全球购物
财务会计专业推荐信
2013/11/30 职场文书
社区七一党员活动方案
2014/01/25 职场文书
歌颂祖国的演讲稿
2014/05/04 职场文书
2014年小学重阳节活动策划方案
2014/09/16 职场文书
优秀教育工作者事迹材料
2014/12/24 职场文书
公司股份转让协议书范本
2015/01/28 职场文书
数学教师求职信范文
2015/03/20 职场文书
大学迎新生的欢迎词
2019/06/25 职场文书
iPhone13再次曝光
2021/04/15 数码科技