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利用openpyxl库遍历Sheet的实例
May 03 Python
python读取xlsx的方法
Dec 25 Python
python 读取鼠标点击坐标的实例
Dec 29 Python
java判断三位数的实例讲解
Jun 10 Python
解决webdriver.Chrome()报错:Message:'chromedriver' executable needs to be in Path
Jun 12 Python
windows环境中利用celery实现简单任务队列过程解析
Nov 29 Python
Python bisect模块原理及常见实例
Jun 17 Python
python else语句在循环中的运用详解
Jul 06 Python
Python实例方法、类方法、静态方法区别详解
Sep 05 Python
详解如何在pyqt中通过OpenCV实现对窗口的透视变换
Sep 20 Python
解决python3输入的坑——input()
Dec 05 Python
requests在python中发送请求的实例讲解
Feb 17 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
php 上传功能实例代码
2010/04/13 PHP
PHP循环函数使用介绍之PHP基础入门教程
2013/09/21 PHP
Javascript的闭包
2009/12/31 Javascript
js split 的用法和定义 js split分割字符串成数组的实例代码
2012/05/13 Javascript
jquery焦点图片切换(数字标注/手动/自动播放/横向滚动)
2013/01/24 Javascript
javascript获取form里的表单元素的示例代码
2014/02/14 Javascript
页面js遇到乱码问题的解决方法是和无法转码的情况
2014/04/30 Javascript
JavaScript实现在数组中查找不同顺序排列的字符串
2014/09/26 Javascript
Clipboard.js 无需Flash的JavaScript复制粘贴库
2015/10/02 Javascript
使用PBFunc在Powerbuilder中支付宝当面付款功能
2016/10/01 Javascript
JAVA Web实时消息后台服务器推送技术---GoEasy
2016/11/04 Javascript
Vue计算属性的使用
2017/08/04 Javascript
利用vue.js实现被选中状态的改变方法
2018/02/08 Javascript
Vue实现底部侧边工具栏的实例代码
2018/09/03 Javascript
Node.js实现简单管理系统
2019/09/23 Javascript
Vue路由守卫之路由独享守卫
2019/09/25 Javascript
element-ui如何防止重复提交的方法步骤
2019/12/09 Javascript
浅谈python字典多键值及重复键值的使用
2016/11/04 Python
python实现剪切功能
2019/01/23 Python
Django多数据库的实现过程详解
2019/08/01 Python
python实现美团订单推送到测试环境,提供便利操作示例
2019/08/09 Python
利用Tensorflow构建和训练自己的CNN来做简单的验证码识别方式
2020/01/20 Python
Python数据可视化实现多种图例代码详解
2020/07/14 Python
Django3中的自定义用户模型实例详解
2020/08/23 Python
python查询MySQL将数据写入Excel
2020/10/29 Python
Python Socket多线程并发原理及实现
2020/12/11 Python
海滩咖啡馆:Beach Cafe
2018/02/02 全球购物
屈臣氏马来西亚官网:Watsons马来西亚
2019/06/15 全球购物
Java软件工程师综合面试题笔试题
2013/09/08 面试题
自考毕业自我鉴定范文
2013/10/27 职场文书
旅游管理专业大学生职业规划书
2014/02/27 职场文书
党员批评与自我批评发言
2014/10/02 职场文书
2016年秋季运动会广播稿
2015/12/21 职场文书
OpenCV3.3+Python3.6实现图片高斯模糊
2021/05/18 Python
浅析Python实现DFA算法
2021/06/26 Python
html+css实现滚动到元素位置显示加载动画效果
2021/08/02 HTML / CSS