Python mutiprocessing多线程池pool操作示例


Posted in Python onJanuary 30, 2019

本文实例讲述了Python mutiprocessing多线程池pool操作。分享给大家供大家参考,具体如下:

python — mutiprocessing 多线程 pool

Python mutiprocessing多线程池pool操作示例

脚本代码:

root@72132server:~/python/multiprocess# ls
multiprocess_pool.py multprocess.py
root@72132server:~/python/multiprocess# cat multiprocess_pool.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import multiprocessing
import sys,os,time
result = []#把运行的进程池放入,空的列表
def run(msg):#定义正在处理进程编号数的函数功能
  print 'threading number:%s %s' %(msg,os.getpid())#打印正在处理的进程编号数与对应的系统进程号
  time.sleep(2)
p = multiprocessing.Pool(processes = 25)#绑定事例,同时执行25个线程
for i in range(100):
  result.append(p.apply_async(run,('%s' %i,)))#异步传输正在运行的进程数字号码
p.close()#关闭正在运行的25个进程
#p.join()
for res in result:#获取运行结果
  res.get(timeout=5)
root@72132server:~/python/multiprocess#

运行情况:

1)脚本运行

root@72132server:~/python/multiprocess# python multiprocess_pool.py
threading number:0 27912
threading number:1 27915
threading number:2 27913
threading number:3 27916
threading number:4 27917
threading number:5 27918
threading number:6 27919
threading number:7 27920
threading number:8 27922
threading number:9 27923
threading number:10 27924
threading number:11 27925
threading number:12 27926
threading number:13 27927
threading number:14 27928
threading number:15 27914
threading number:16 27929
threading number:17 27921
threading number:18 27930
threading number:19 27931
threading number:20 27932
threading number:21 27934
threading number:22 27935
threading number:23 27936
threading number:24 27933
threading number:25 27912
threading number:26 27915
threading number:27 27917
threading number:28 27918
threading number:29 27916
threading number:30 27913
threading number:31 27922
threading number:32 27919
threading number:33 27920
threading number:34 27923
threading number:35 27924
threading number:36 27925
threading number:37 27927
threading number:38 27921
threading number:39 27930
threading number:40 27932
threading number:41 27934
threading number:42 27935
threading number:43 27926
threading number:44 27931
threading number:45 27928
threading number:46 27929
threading number:47 27914
threading number:48 27933
threading number:49 27936
threading number:50 27912
threading number:51 27915

2)进程查看(25个进程同时运行)

root@72132server:~/python/multiprocess# ps -ef | grep multi
root   27905 23930 0 22:39 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   27911 20609 1 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27912 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27913 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27914 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27915 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27916 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27917 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27918 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27919 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27920 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27921 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27922 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27923 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27924 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27925 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27926 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27927 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27928 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27929 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27930 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27931 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27932 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27933 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27934 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27935 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27936 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27941 23930 0 22:39 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   27911 20609 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27912 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27913 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27914 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27915 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27916 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27917 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27918 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27919 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27920 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27921 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27922 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27923 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27924 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27925 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27926 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27927 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27928 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27929 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27930 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27931 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27932 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27933 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27934 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27935 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27936 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27943 23930 0 22:39 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess#

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

Python 相关文章推荐
跟老齐学Python之模块的加载
Oct 24 Python
用实例解释Python中的继承和多态的概念
Apr 27 Python
Python中的命令行参数解析工具之docopt详解
Mar 27 Python
Python实现的HMacMD5加密算法示例
Apr 03 Python
Django 多语言教程的实现(i18n)
Jul 07 Python
浅谈python编译pyc工程--导包问题解决
Mar 20 Python
NumPy 数组使用大全
Apr 25 Python
Python箱型图绘制与特征值获取过程解析
Oct 22 Python
Python调用graphviz绘制结构化图形网络示例
Nov 22 Python
python 初始化一个定长的数组实例
Dec 02 Python
Lombok插件安装(IDEA)及配置jar包使用详解
Nov 04 Python
Python中的流程控制详解
Feb 18 Python
Python多线程处理实例详解【单进程/多进程】
Jan 30 #Python
Python读取Pickle文件信息并计算与当前时间间隔的方法分析
Jan 30 #Python
python实现烟花小程序
Jan 30 #Python
Python面向对象程序设计示例小结
Jan 30 #Python
python实现浪漫的烟花秀
Jan 30 #Python
新年快乐! python实现绚烂的烟花绽放效果
Jan 30 #Python
python+selenium 定位到元素,无法点击的解决方法
Jan 30 #Python
You might like
php mysql数据库操作类
2008/06/04 PHP
老生常谈PHP位运算的用途
2017/03/12 PHP
PHP fopen中文文件名乱码问题解决方案
2020/10/28 PHP
javascript插入样式实现代码
2012/02/22 Javascript
JavaScript中的noscript元素属性位置及作用介绍
2013/04/11 Javascript
node.js+express制作网页计算器
2016/01/17 Javascript
浅析函数声明和函数表达式——函数声明的声明提前
2016/05/03 Javascript
JS原型对象的创建方法详解
2016/06/16 Javascript
JS检测页面中哪个HTML标签触发点击事件的方法
2016/06/17 Javascript
JavaScript中ES6字符串扩展方法
2016/08/26 Javascript
JS实现JSON.stringify的实例代码讲解
2017/02/07 Javascript
jQuery插件HighCharts绘制的2D堆柱状图效果示例【附demo源码下载】
2017/03/14 Javascript
动态加载权限管理模块中的Vue组件
2018/01/16 Javascript
vue项目实现github在线预览功能
2018/06/20 Javascript
JavaScript面向对象中接口实现方法详解
2019/07/24 Javascript
uni-app实现点赞评论功能
2019/11/25 Javascript
Vue管理系统前端之组件拆分封装详解
2020/08/23 Javascript
python用字典统计单词或汉字词个数示例
2014/04/22 Python
python进阶教程之循环对象
2014/08/30 Python
Python打印scrapy蜘蛛抓取树结构的方法
2015/04/08 Python
Python3读取文件常用方法实例分析
2015/05/22 Python
Linux 发邮件磁盘空间监控(python)
2016/04/23 Python
Python增强赋值和共享引用注意事项小结
2019/05/28 Python
python执行scp命令拷贝文件及文件夹到远程主机的目录方法
2019/07/08 Python
Python3爬虫关于识别点触点选验证码的实例讲解
2020/07/30 Python
Django DRF APIView源码运行流程详解
2020/08/17 Python
如何使用PyCharm引入需要使用的包的方法
2020/09/22 Python
关于box-sizing的全面理解
2016/07/28 HTML / CSS
泰坦健身器材:Titan Fitness
2018/02/13 全球购物
Ibood荷兰:互联网每日最佳在线优惠
2019/02/28 全球购物
毕业生自我鉴定范文
2013/11/08 职场文书
日语专业推荐信
2013/11/12 职场文书
最新奶茶店创业计划书
2014/01/25 职场文书
卫生系统先进事迹
2014/05/13 职场文书
工程部岗位职责
2015/02/10 职场文书
送给教师们,到底该如何写好教学反思?
2019/07/02 职场文书