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读写文件操作示例程序
Dec 02 Python
Python异常处理总结
Aug 15 Python
Python中模拟enum枚举类型的5种方法分享
Nov 22 Python
python中循环语句while用法实例
May 16 Python
python实现决策树分类算法
Dec 21 Python
Python字典操作详细介绍及字典内建方法分享
Jan 04 Python
python初学者,用python实现基本的学生管理系统(python3)代码实例
Apr 10 Python
树莓派+摄像头实现对移动物体的检测
Jun 22 Python
TensorFlow加载模型时出错的解决方式
Feb 06 Python
Python tkinter界面实现历史天气查询的示例代码
Aug 23 Python
pycharm专业版远程登录服务器的详细教程
Sep 15 Python
详解Python中string模块除去Str还剩下什么
Nov 30 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与XML、XSLT、Mysql的结合运用实现代码
2009/11/19 PHP
生成ubuntu自动切换壁纸xml文件的php代码
2010/07/17 PHP
解析:php调用MsSQL存储过程使用内置RETVAL获取过程中的return值
2013/07/03 PHP
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法
2016/05/19 PHP
php生成二维码图片方法汇总
2016/12/17 PHP
浅谈PHPANALYSIS提取关键字
2019/03/08 PHP
javascript 打印页面代码
2009/03/24 Javascript
页面调用单个swf文件,嵌套出多个方法。
2011/11/21 Javascript
jquery验证表单中的单选与多选实例
2013/08/18 Javascript
JavaScript使用setInterval()函数实现简单轮询操作的方法
2015/02/02 Javascript
JS实现文字链接感应鼠标淡入淡出改变颜色的方法
2015/02/26 Javascript
使用AngularJS 应用访问 Android 手机的图片库
2015/03/24 Javascript
jQuery+CSS3文字跑马灯特效的简单实现
2016/06/25 Javascript
JavaScript基础知识点归纳(推荐)
2016/07/09 Javascript
jquery实现的回旋滚动效果完整实例【附demo源码下载】
2016/09/20 Javascript
Vue2.0 多 Tab切换组件的封装实例
2017/07/28 Javascript
JS关于刷新页面的相关总结
2018/05/09 Javascript
利用Vue构造器创建Form组件的通用解决方法
2018/12/03 Javascript
vue中axios的二次封装实例讲解
2019/10/14 Javascript
[45:59]EG vs OG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
利用Python获取赶集网招聘信息前篇
2016/04/18 Python
Python学习小技巧之利用字典的默认行为
2017/05/20 Python
Python使用pandas处理CSV文件的实例讲解
2018/06/22 Python
Python 如何优雅的将数字转化为时间格式的方法
2019/09/26 Python
python统计函数库scipy.stats的用法解析
2020/02/25 Python
python目标检测给图画框,bbox画到图上并保存案例
2020/03/10 Python
python Matplotlib模块的使用
2020/09/16 Python
python爬虫搭配起Bilibili唧唧的流程分析
2020/12/01 Python
pandas 按日期范围筛选数据的实现
2021/02/20 Python
利用CSS3实现文本框的清除按钮相关的一些效果
2015/06/23 HTML / CSS
英国著名的美容护肤和护发产品购物网站:Lookfantastic
2020/11/23 全球购物
Java里面如何创建一个内部类的实例
2015/01/19 面试题
部门2015年度工作总结
2015/04/29 职场文书
2015银行年终工作总结范文
2015/05/26 职场文书
优胜劣汰,强者为王——读《鲁滨逊漂流记》有感
2019/08/15 职场文书
《追风筝的人》:人心中的成见是座大山,但请不忘初心
2019/11/15 职场文书