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实现360的字符显示界面
Feb 21 Python
Python中的字典与成员运算符初步探究
Oct 13 Python
Python2和Python3中print的用法示例总结
Oct 25 Python
python 保存float类型的小数的位数方法
Oct 17 Python
python3 打开外部程序及关闭的示例
Nov 06 Python
pandas DataFrame索引行列的实现
Jun 04 Python
python实现关闭第三方窗口的方法
Jun 28 Python
使用python serial 获取所有的串口名称的实例
Jul 02 Python
简单了解python shutil模块原理及使用方法
Apr 28 Python
用python给csv里的数据排序的具体代码
Jul 17 Python
Python同时处理多个异常的方法
Jul 28 Python
教你使用Sublime text3搭建Python开发环境及常用插件安装另分享Sublime text3最新激活注册码
Nov 12 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检测字符串是否为UTF8编码的常用方法
2014/11/21 PHP
php实现文本数据导入SQL SERVER
2015/05/17 PHP
javascript 特殊字符串
2009/02/25 Javascript
JQuery动画和停止动画实例代码
2013/03/01 Javascript
javascript重写alert方法的实例代码
2013/03/29 Javascript
IE6下拉框图层问题探讨及解决
2014/01/03 Javascript
javascript删除字符串最后一个字符
2014/01/14 Javascript
Javascript基础教程之数据类型 (字符串 String)
2015/01/18 Javascript
浏览器兼容性问题大汇总
2015/12/17 Javascript
用canvas 实现个图片三角化(LOW POLY)效果
2016/02/18 Javascript
原生JS和jQuery版实现文件上传功能
2016/04/18 Javascript
分享vue里swiper的一些坑
2018/08/30 Javascript
jquery实现动态创建form并提交的方法示例
2019/05/27 jQuery
vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)
2020/03/07 Javascript
Javascript组合继承方法代码实例解析
2020/04/02 Javascript
jQuery使用jsonp实现百度搜索的示例代码
2020/07/08 jQuery
利用vue3+ts实现管理后台(增删改查)
2020/10/30 Javascript
python 获取本机ip地址的两个方法
2013/02/25 Python
如何搜索查找并解决Django相关的问题
2014/06/30 Python
Python切换pip安装源的方法详解
2016/11/18 Python
python实现简易通讯录修改版
2018/03/13 Python
python中实现字符串翻转的方法
2018/07/11 Python
python调用tcpdump抓包过滤的方法
2018/07/18 Python
Python正则表达式和元字符详解
2018/11/29 Python
Python爬虫学习之获取指定网页源码
2019/07/30 Python
基于python实现蓝牙通信代码实例
2019/11/19 Python
python“静态”变量、实例变量与本地变量的声明示例
2020/11/13 Python
美国Rue La La闪购网站:奢侈品、中高档品牌限时折扣
2016/10/19 全球购物
仪器仪表检测毕业生自荐信
2013/10/31 职场文书
技能比赛获奖感言
2014/02/14 职场文书
农行心得体会
2014/09/02 职场文书
信用卡逾期证明示例
2014/09/13 职场文书
2016圣诞节贺卡寄语
2015/12/07 职场文书
学习新党章心得体会2016
2016/01/15 职场文书
Win11怎么启动任务管理器?Win11启动任务管理器的几种方法
2021/11/23 数码科技
nginx容器方式反向代理实战
2022/04/18 Servers