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 相关文章推荐
apache部署python程序出现503错误的解决方法
Jul 24 Python
Python爬取当当、京东、亚马逊图书信息代码实例
Dec 09 Python
numpy数组拼接简单示例
Dec 15 Python
Python温度转换实例分析
Jan 17 Python
python读取TXT每行,并存到LIST中的方法
Oct 26 Python
nohup后台启动Python脚本,log不刷新的解决方法
Jan 14 Python
selenium python 实现基本自动化测试的示例代码
Feb 25 Python
numpy数组之存取文件的实现示例
May 24 Python
浅析python 中大括号中括号小括号的区分
Jul 29 Python
pyinstaller打包程序exe踩过的坑
Nov 19 Python
Django ORM实现按天获取数据去重求和例子
May 18 Python
matplotlib常见函数之plt.rcParams、matshow的使用(坐标轴设置)
Jan 05 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中Date获取时间不正确怎么办
2008/06/05 PHP
smarty表格换行实例
2014/12/15 PHP
php类常量用法实例分析
2015/07/09 PHP
PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法
2017/06/20 PHP
PHP实现普通hash分布式算法简单示例
2018/08/06 PHP
laravel5.5安装jwt-auth 生成token令牌的示例
2019/10/24 PHP
显示、隐藏密码
2006/07/01 Javascript
javascript对下拉列表框(select)的操作实例讲解
2013/11/29 Javascript
D3.js 从P元素的创建开始(显示可加载数据)
2014/10/30 Javascript
如何解决easyui自定义标签 datagrid edit combobox 手动输入保存不上
2015/12/26 Javascript
详解Weex基于Vue2.0开发模板搭建
2017/03/20 Javascript
全面解析Node.js 8 重要功能和修复
2017/06/02 Javascript
React Native 搭建开发环境的方法步骤
2017/10/30 Javascript
Vue-Router实现组件间跳转的三种方法
2017/11/07 Javascript
JavaScript实现修改伪类样式
2017/11/27 Javascript
HTML5+JS+JQuery+ECharts实现异步加载问题
2017/12/16 jQuery
node.js实现为PDF添加水印的示例代码
2018/12/05 Javascript
详解puppeteer使用代理
2018/12/27 Javascript
关于layui的动态图标不显示的解决方法
2019/09/04 Javascript
微信小程序图片自适应实现解析
2020/01/21 Javascript
Python如何实现文本转语音
2016/08/08 Python
python计算列表内各元素的个数实例
2018/06/29 Python
python3转换code128条形码的方法
2019/04/17 Python
Python类的绑定方法和非绑定方法实例解析
2020/03/04 Python
Python基于tkinter canvas实现图片裁剪功能
2020/11/05 Python
python编写扎金花小程序的实例代码
2021/02/23 Python
html5手机键盘弹出收起的处理
2020/01/20 HTML / CSS
英国第一家领先的在线处方眼镜零售商:Glasses Direct
2018/02/23 全球购物
工业自动化专业自荐信范文
2014/04/10 职场文书
《金孔雀轻轻跳》教学反思
2014/04/20 职场文书
环保建议书300字
2014/05/14 职场文书
企业安全标语
2014/06/07 职场文书
高一课前三分钟演讲稿
2014/09/13 职场文书
环境卫生标语
2015/08/03 职场文书
幼儿园2016圣诞节活动总结
2016/03/31 职场文书
MySQL优化常用的19种有效方法(推荐!)
2022/03/17 MySQL