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根据路径导入模块的方法
Sep 30 Python
Python中使用pprint函数进行格式化输出的教程
Apr 07 Python
Python中用sleep()方法操作时间的教程
May 22 Python
谈谈Python进行验证码识别的一些想法
Jan 25 Python
使用Python对Csv文件操作实例代码
May 12 Python
Python 12306抢火车票脚本
Feb 07 Python
Python代码实现删除一个list里面重复元素的方法
Apr 02 Python
python判断所输入的任意一个正整数是否为素数的两种方法
Jun 27 Python
Pandas0.25来了千万别错过这10大好用的新功能
Aug 07 Python
python求前n个阶乘的和实例
Apr 02 Python
工程师必须了解的LRU缓存淘汰算法以及python实现过程
Oct 15 Python
python3美化表格数据输出结果的实现代码
Apr 14 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
模拟SQLSERVER的两个函数:dateadd(),datediff()
2006/10/09 PHP
PHP中通过语义URL防止网站被攻击的方法分享
2011/09/08 PHP
PHP中数字检测is_numeric与ctype_digit的区别介绍
2012/10/04 PHP
PHP实现统计在线人数功能示例
2016/10/15 PHP
Laravel网站打开速度优化的方法汇总
2017/07/16 PHP
thinkphp5 加载静态资源路径与常量的方法
2017/12/24 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
2018/10/15 PHP
php ActiveMQ的安装与使用方法图文教程
2020/02/23 PHP
jquery选择器原理介绍($()使用方法)
2014/03/25 Javascript
js style动态设置table高度
2014/10/21 Javascript
分享经典的JavaScript开发技巧
2015/11/21 Javascript
Javascript原型链的原理详解
2016/01/05 Javascript
jQuery实现图片走马灯效果的原理分析
2016/01/16 Javascript
浅析Javascript中bind()方法的使用与实现
2016/04/29 Javascript
如何判断Javascript对象是否存在的简单实例
2016/05/18 Javascript
jQuery焦点图轮播效果实现方法
2016/12/19 Javascript
零基础轻松学JavaScript闭包
2016/12/30 Javascript
Vue框架中正确引入JS库的方法介绍
2017/07/30 Javascript
微信小程序项目实践之九宫格实现及item跳转功能
2018/07/19 Javascript
vue中是怎样监听数组变化的
2020/10/24 Javascript
python实现汉诺塔递归算法经典案例
2021/03/01 Python
Python 操作文件的基本方法总结
2017/08/10 Python
正确理解Python中if __name__ == '__main__'
2019/01/24 Python
解决keras,val_categorical_accuracy:,0.0000e+00问题
2020/07/02 Python
Django restful framework生成API文档过程详解
2020/11/12 Python
CSS3制作hover下划线动画
2017/03/27 HTML / CSS
html5跳转小程序wx-open-launch-weapp踩坑
2020/12/02 HTML / CSS
Perfume’s Club澳大利亚官网:西班牙领先的在线美容店
2021/02/01 全球购物
服务之星获奖感言
2014/01/21 职场文书
客户经理竞聘演讲稿
2014/05/15 职场文书
2014流动人口计划生育工作总结
2014/12/20 职场文书
社区好人好事材料
2014/12/26 职场文书
二婚主持词
2015/06/30 职场文书
法制教育主题班会
2015/08/13 职场文书
粗暴解决CUDA out of memory的问题
2021/05/22 Python
Apache SeaTunnel实现 非CDC数据抽取
2022/05/20 Servers