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实现拉钩网上的FizzBuzzWhizz问题示例
May 05 Python
python使用any判断一个对象是否为空的方法
Nov 19 Python
python实现带验证码网站的自动登陆实现代码
Jan 12 Python
python字符串与url编码的转换实例
May 10 Python
python面向对象入门教程之从代码复用开始(一)
Dec 11 Python
Python3 pandas 操作列表实例详解
Sep 23 Python
PyTorch 解决Dataset和Dataloader遇到的问题
Jan 08 Python
从训练好的tensorflow模型中打印训练变量实例
Jan 20 Python
Django多数据库配置及逆向生成model教程
Mar 28 Python
使用jupyter notebook运行python和R的步骤
Aug 13 Python
python获取淘宝服务器时间的代码示例
Apr 22 Python
在pycharm中无法import所安装的库解决方案
May 31 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
用DBSQL类加快开发MySQL数据库程序的速度
2006/10/09 PHP
PHP字符转义相关函数小结(php下的转义字符串)
2007/04/12 PHP
PHP文件上传判断file是否己选择上传文件的方法
2014/11/10 PHP
PHP合并数组函数array_merge用法分析
2017/02/17 PHP
PHP生成(支持多模板)二维码海报代码
2018/04/30 PHP
JavaScript高级程序设计 DOM学习笔记
2011/09/10 Javascript
Jquery 获取对象的几种方式介绍
2014/01/17 Javascript
基于JavaScript实现表单密码的隐藏和显示出来
2016/03/02 Javascript
javascript对象的相关操作小结
2016/05/16 Javascript
AngularJS 指令详细介绍
2016/07/27 Javascript
JavaScript中子对象访问父对象的方式详解
2016/09/01 Javascript
jquery判断iPhone、Android设备类型
2016/09/14 Javascript
区别JavaScript函数声明与变量声明
2018/09/12 Javascript
图片文字识别(OCR)插件Ocrad.js教程
2018/11/26 Javascript
js实现通过开始结束控制的计时器
2019/02/25 Javascript
浅谈js闭包理解
2019/04/01 Javascript
jQuery实现轮播图源码
2019/10/23 jQuery
微信小程序canvas分享海报功能
2019/10/31 Javascript
vue.js+ElementUI实现进度条提示密码强度效果
2020/01/18 Javascript
详解vite2.0配置学习(typescript版本)
2021/02/25 Javascript
python 循环while和for in简单实例
2016/08/16 Python
Python实现简易Web爬虫详解
2018/01/03 Python
python字符串替换re.sub()实例解析
2020/02/09 Python
Python高并发和多线程有什么关系
2020/11/14 Python
pandas map(),apply(),applymap()区别解析
2021/02/24 Python
浅谈HTML5 defer和async的区别
2016/06/07 HTML / CSS
如何使用localstorage代替cookie实现跨域共享数据问题
2018/04/18 HTML / CSS
JACK & JONES英国官方网站:欧洲领先的男装生产商
2017/09/27 全球购物
美国领先的机场停车聚合商:Airport Parking Reservations
2020/02/28 全球购物
你常见到的runtime exception
2016/09/05 面试题
J2EE相关知识面试题
2013/08/26 面试题
作文批改评语大全
2014/04/23 职场文书
爱国主义教育活动总结
2014/05/07 职场文书
师德师风个人自我剖析材料
2014/09/27 职场文书
死者家属慰问信
2015/03/24 职场文书
CSS布局之浮动(float)和定位(position)属性的区别
2021/09/25 HTML / CSS