Python多线程处理实例详解【单进程/多进程】


Posted in Python onJanuary 30, 2019

本文实例讲述了Python多线程处理操作。分享给大家供大家参考,具体如下:

python — 多线程处理

1、一个进程执行完后,继续下一个进程

root@72132server:~# cd /root/python/multiprocess/
root@72132server:~/python/multiprocess# ls
multprocess.py
root@72132server:~/python/multiprocess# cat multprocess.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from multiprocessing import Process,Lock#启用多进程,与进程锁
import time,os
def sayhi(i):
  print 'hello world!!!', i
  time.sleep(10)
#lock = Lock()
for n in range(100):#执行n=100次
  p = Process(target=sayhi,args=(n,))#调用函数def,若def函数里面有参数,就是使用args带值赋值,若函数没有参数的话就args()为空。
  p.start()
  p.join()#一个进程结束才会继续下一个进程。如果注释这句意思是一百个进程同时发起
root@72132server:~/python/multiprocess#

运行情况:

1)进程查看

root@72132server:~# cd /root/python/multiprocess/
root@72132server:~/python/multiprocess# ls
multprocess.py
root@72132server:~/python/multiprocess# vi multprocess.py
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24064 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24066 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24069 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24071 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24073 23930 0 20:46 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24075 23930 0 20:46 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess#

2)脚本运行

root@72132server:~/python/multiprocess# vi multprocess.py
root@72132server:~/python/multiprocess# python multprocess.py
hello world!!! 0
hello world!!! 1
hello world!!! 2
hello world!!! 3
hello world!!! 4
hello world!!! 5
hello world!!! 6
hello world!!! 7
hello world!!! 8
hello world!!! 9
hello world!!! 10
hello world!!! 11

2、100个进行同时运行

root@72132server:~/python/multiprocess# ls
multprocess.py
root@72132server:~/python/multiprocess# cat multprocess.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from multiprocessing import Process,Lock#启用多进程,与进程锁
import time,os
def sayhi(i):
  print 'hello world!!!', i
  time.sleep(10)
#lock = Lock()
for n in range(100):#执行n=100次
  p = Process(target=sayhi,args=(n,))#调用函数def,若def函数里面有参数,就是使用args带值赋值,若函数没有参数的话就args()为空。
  p.start()
  p.join()#一个进程结束才会继续下一个进程。如果注释这句意思是一百个进程同时发起
root@72132server:~/python/multiprocess#
root@72132server:~/python/multiprocess# vi multprocess.py
root@72132server:~/python/multiprocess# cat multprocess.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from multiprocessing import Process,Lock#启用多进程,与进程锁
import time,os
def sayhi(i):
  print 'hello world!!!', i
  time.sleep(10)
#lock = Lock()
for n in range(100):#执行n=100次
  p = Process(target=sayhi,args=(n,))#调用函数def,若def函数里面有参数,就是使用args带值赋值,若函数没有参数的话就args()为空。
  p.start()
  #p.join()#一个进程结束才会继续下一个进程。如果注释这句意思是一百个进程同时发起
root@72132server:~/python/multiprocess#

运行情况

1)进程查看

Python多线程处理实例详解【单进程/多进程】

2)脚本运行(1秒跑完)

root@72132server:~/python/multiprocess# python multprocess.py
hello world!!! 0
hello world!!! 2
hello world!!! 3
hello world!!! 5
hello world!!! 7
hello world!!! 8
hello world!!! 6
hello world!!! 9
hello world!!! 10
hello world!!! 11
hello world!!! 14
hello world!!! 4
hello world!!! 15
hello world!!! 16
hello world!!! 1
hello world!!! 13
hello world!!! 18
hello world!!! 20
hello world!!! 19
hello world!!! 21
hello world!!! 12
hello world!!! 17
hello world!!! 23
hello world!!! 24
hello world!!! 26
hello world!!! 27
hello world!!! 22
hello world!!! 29
hello world!!! 31
hello world!!! 32
hello world!!! 33
hello world!!! 34
hello world!!! 28
hello world!!! 25
hello world!!! 30
hello world!!! 35
hello world!!! 36
hello world!!! 39
hello world!!! 41
hello world!!! 37
hello world!!! 40
hello world!!! 42
hello world!!! 43
hello world!!! 46
hello world!!! 47
hello world!!! 48
hello world!!! 38
hello world!!! 44
hello world!!! 45
hello world!!! 50
hello world!!! 51
hello world!!! 53
hello world!!! 54
hello world!!! 55
hello world!!! 57
hello world!!! 49
hello world!!! 58
hello world!!! 59
hello world!!! 60
hello world!!! 61
hello world!!! 62
hello world!!! 63
hello world!!! 64
hello world!!! 65
hello world!!! 66
hello world!!! 67
hello world!!! 68
hello world!!! 69
hello world!!! 56
hello world!!! 70
hello world!!! 52
hello world!!! 71
hello world!!! 72
hello world!!! 73
hello world!!! 76
hello world!!! 74
hello world!!! 78
hello world!!! 79
hello world!!! 80
hello world!!! 82
hello world!!! 77
hello world!!! 83
hello world!!! 84
hello world!!! 85
hello world!!! 86
hello world!!! 87
hello world!!! 81
hello world!!! 91
hello world!!! 75
hello world!!! 89
hello world!!! 92
hello world!!! 88
hello world!!! 90
hello world!!! 93
hello world!!! 95
hello world!!! 94
hello world!!! 96
hello world!!! 98
hello world!!! 9

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python break语句详解
Mar 11 Python
深入理解python中的atexit模块
Mar 07 Python
python实现杨辉三角思路
Jul 14 Python
Python使用装饰器模拟用户登陆验证功能示例
Aug 24 Python
详解Python进阶之切片的误区与高级用法
Dec 24 Python
Python3.5 Pandas模块缺失值处理和层次索引实例详解
Apr 23 Python
Python中函数参数匹配模型详解
Jun 09 Python
简单了解python高阶函数map/reduce
Jun 28 Python
pyqt5、qtdesigner安装和环境设置教程
Sep 25 Python
Python求正态分布曲线下面积实例
Nov 20 Python
Django模型中字段属性choice使用说明
Mar 30 Python
利用Python批量识别电子账单数据的方法
Feb 08 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
解决Python selenium get页面很慢时的问题
Jan 30 #Python
You might like
对盗链说再见...
2006/10/09 PHP
使用php4加速网络传输
2006/10/09 PHP
PHP分页函数代码(简单实用型)
2010/12/02 PHP
discuz的php防止sql注入函数
2011/01/17 PHP
PHP中file_exists使用中遇到的问题小结
2016/04/05 PHP
PHP内置函数生成随机数实例
2019/01/18 PHP
PHP用swoole+websocket和redis实现web一对一聊天
2019/11/05 PHP
php-7.3.6 编译安装过程
2020/02/11 PHP
js 弹出框 替代浏览器的弹出框
2010/10/29 Javascript
输入密码检测大写是否锁定js实现代码
2012/12/03 Javascript
js实现DOM走马灯特效的方法
2015/01/21 Javascript
JavaScript中的replace()方法使用详解
2015/06/06 Javascript
jquery实现倒计时功能
2015/12/28 Javascript
js实现网页图片延时加载 提升网页打开速度
2016/01/26 Javascript
JS创建对象的写法示例
2016/11/04 Javascript
关于jQuery EasyUI 中刷新Tab选项卡后一个页面变形的解决方法
2017/03/02 Javascript
JavaScript实现图片切换效果
2017/08/12 Javascript
angular1配合gulp和bower的使用教程
2018/01/19 Javascript
node.js文件上传重命名以及移动位置的示例代码
2018/01/19 Javascript
ios设备中angularjs无法改变页面title的解决方法
2018/09/13 Javascript
Layui实现带查询条件的分页
2019/07/27 Javascript
详解利用nodejs对本地json文件进行增删改查
2019/09/20 NodeJs
[43:35]EG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
详解Python中的变量及其命名和打印
2016/03/11 Python
解决pycharm 工具栏Tool中找不到Run manager.py Task的问题
2019/07/01 Python
django的model操作汇整详解
2019/07/26 Python
pytorch梯度剪裁方式
2020/02/04 Python
美国学校用品、教室和教学商店:Discount School Supply
2018/04/04 全球购物
高校教师思想汇报
2014/01/11 职场文书
校园十佳歌手策划书
2014/01/22 职场文书
小学体育教学反思
2014/01/31 职场文书
美食节策划方案
2014/05/26 职场文书
单位委托书
2014/10/15 职场文书
2016幼儿园教师节新闻稿
2015/11/25 职场文书
opencv深入浅出了解机器学习和深度学习
2022/03/17 Python
TV动画《间谍过家家》公开PV
2022/03/20 日漫