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的Django框架的运行方式及处理流程
Apr 08 Python
python中查看变量内存地址的方法
May 05 Python
深入解析Python中的descriptor描述器的作用及用法
Jun 27 Python
python与php实现分割文件代码
Mar 06 Python
遗传算法之Python实现代码
Oct 10 Python
python OpenCV学习笔记直方图反向投影的实现
Feb 07 Python
Python 中导入csv数据的三种方法
Nov 01 Python
使用CodeMirror实现Python3在线编辑器的示例代码
Jan 14 Python
详解DeBug Python神级工具PySnooper
Jul 03 Python
Flask教程之重定向与错误处理实例分析
Aug 01 Python
Python2 与Python3的版本区别实例分析
Mar 30 Python
Python之字符串的遍历的4种方式
Dec 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
全国FM电台频率大全 - 4 山西省
2020/03/11 无线电
PHP 文件缓存的性能测试
2010/04/25 PHP
php for 循环使用的简单实例
2016/06/02 PHP
php常用的工具开发整理
2019/09/26 PHP
ExtJS 2.0 实用简明教程之布局概述
2009/04/29 Javascript
nodejs 后缀名判断限制代码
2011/03/31 NodeJs
jquery 多行文本框(textarea)高度变化
2013/07/03 Javascript
Jquery显示和隐藏元素或设为只读(含Ligerui的控件禁用,实例说明介绍)
2013/07/09 Javascript
JS对话框_JS模态对话框showModalDialog用法总结
2014/01/11 Javascript
jQuery中Ajax的get、post等方法详解
2015/01/20 Javascript
jQuery搜索同辈元素方法
2015/02/10 Javascript
老生常谈Bootstrap媒体对象
2017/07/06 Javascript
Vue学习笔记进阶篇之单元素过度
2017/07/19 Javascript
深入理解React高阶组件
2017/09/28 Javascript
Vue2.0 http请求以及loading展示实例
2018/03/06 Javascript
jQuery 实现倒计时天,时,分,秒功能
2018/07/31 jQuery
vue移动端html5页面根据屏幕适配的四种解决方法
2018/10/19 Javascript
react 中父组件与子组件双向绑定问题
2019/05/20 Javascript
vue实现日历备忘录功能
2020/09/24 Javascript
浅谈python中列表、字符串、字典的常用操作
2017/09/19 Python
python实现微信每日一句自动发送给喜欢的人
2019/04/29 Python
keras分类模型中的输入数据与标签的维度实例
2020/07/03 Python
浅谈对python中if、elif、else的误解
2020/08/20 Python
需要知道的CSS3动画技术
2010/01/01 HTML / CSS
css3实现简单的白云飘动背景特效
2020/10/28 HTML / CSS
英国体育器材进口商店:UK Sport Imports
2017/03/14 全球购物
美国批发供应商:Kole Imports
2019/04/10 全球购物
营销团队口号
2014/06/06 职场文书
六一儿童节活动总结
2014/08/27 职场文书
文体活动总结
2015/02/04 职场文书
老龙头导游词
2015/02/11 职场文书
2015秋季新学期开学寄语
2015/05/28 职场文书
2015年教师个人业务工作总结
2015/10/23 职场文书
三好学生主要事迹怎么写
2015/11/03 职场文书
2016教师节感恩话语
2015/12/09 职场文书
2019大学生实习报告
2019/06/21 职场文书