python线程池如何实现同步(Python mutiprocessing多线程池pool操作示例)
类别:脚本大全 浏览量:1736
时间:2022-04-01 21:02:43 python线程池如何实现同步
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程序设计有所帮助。
原文链接:https://blog.csdn.net/xwbk12/article/details/77624248
您可能感兴趣
- python闭包的讲解(详解python函数的闭包问题内部函数与外部函数详述)
- 如何查看python是否安装selenium(selenium+python截图不成功的解决方法)
- python3.7不兼容pywinauto(浅谈python编译pyc工程--导包问题解决)
- python基础编程函数参数(详解Python 函数如何重载?)
- python中tkinter模块窗口操作(详解python tkinter教程-事件绑定)
- python的环境变量怎么设置(详解如何设置Python环境变量?)
- python2.7中logging的使用方式(Python中使用logging和traceback模块记录日志和跟踪异常)
- python与php(解决Python3 被PHP程序调用执行返回乱码的问题)
- python抖音视频收集(python制作抖音代码舞)
- python用于机器人(python机器人运动范围问题的解答)
- python实现螺旋矩阵(Python3实现的旋转矩阵图像算法示例)
- python自动处理图片(python制作图片缩略图)
- python外部如何调嵌套函数(python中嵌套函数的实操步骤)
- python画折线图(python使用Plotly绘图工具绘制水平条形图)
- python虚拟环境和包使用教程(在win10和linux上分别安装Python虚拟环境的方法步骤)
- python递归深度遍历多叉树(Python实现二叉树的常见遍历操作总结7种方法)
- 为什么现在社会越来越卷了(现在社会为什么发展那么快呢)
- 直播带货能赚到很多钱吗(直播带货能赚到很多钱吗现在)
- 做网红真的很能赚钱吗(做网红真的很能赚钱吗)
- 10句英语常用(英语常用900句)
- 爱情能当饭吃吗(爱情能当饭吃吗说说)
- 白T恤穿法(白t恤)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9