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中的继承和多态的概念
Apr 27 Python
python实现FTP服务器服务的方法
Apr 11 Python
K-means聚类算法介绍与利用python实现的代码示例
Nov 13 Python
Python多线程threading和multiprocessing模块实例解析
Jan 29 Python
详解Django+uwsgi+Nginx上线最佳实战
Mar 14 Python
python issubclass 和 isinstance函数
Jul 25 Python
Python虚拟环境库virtualenvwrapper安装及使用
Jun 17 Python
使用Dajngo 通过代码添加xadmin用户和权限(组)
Jul 03 Python
Python同时处理多个异常的方法
Jul 28 Python
如何基于python实现年会抽奖工具
Oct 20 Python
python asyncio 协程库的使用
Jan 21 Python
Python实现简单的猜单词
Jun 15 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 dirname()与__FILE__常量的应用
2013/06/24 PHP
PHP数组操作类实例
2015/07/11 PHP
php生成高清缩略图实例详解
2015/12/07 PHP
PHP编程计算日期间隔天数的方法
2017/04/26 PHP
javascript 对象比较实现代码
2009/04/27 Javascript
cument.execCommand()用法深入理解
2012/12/04 Javascript
jquery操作HTML5 的data-*的用法实例分享
2014/08/17 Javascript
js面向对象之静态方法和静态属性实例分析
2015/01/10 Javascript
深入理解JavaScript系列(31):设计模式之代理模式详解
2015/03/03 Javascript
javascript引用类型指针的工作方式
2015/04/13 Javascript
JS实现仿雅虎首页快捷登录入口及导航模块效果
2015/09/19 Javascript
javascript实现网站加入收藏功能
2015/12/16 Javascript
Jquery实现上下移动和排序代码
2016/10/17 Javascript
微信小程序page的生命周期和音频播放及监听实例详解
2017/04/07 Javascript
详解前后端分离之VueJS前端
2017/05/24 Javascript
js getBoundingClientRect使用方法详解
2019/07/17 Javascript
Echarts地图添加引导线效果(labelLine)
2019/09/30 Javascript
vue实现路由不变的情况下,刷新页面操作示例
2020/02/02 Javascript
[01:02:32]DOTA2-DPC中国联赛 正赛 iG vs PSG.LGD BO3 第二场 2月26日
2021/03/11 DOTA
教大家使用Python SqlAlchemy
2016/02/12 Python
Python基础教程之浅拷贝和深拷贝实例详解
2017/07/15 Python
python实现字符串和字典的转换
2018/09/29 Python
通过实例了解Python str()和repr()的区别
2020/01/17 Python
pandas中read_csv、rolling、expanding用法详解
2020/04/21 Python
python如何导出微信公众号文章方法详解
2020/08/31 Python
Python descriptor(描述符)的实现
2020/11/15 Python
CSS3 background-image颜色渐变的实现代码
2018/09/13 HTML / CSS
维多利亚的秘密官方旗舰店:VICTORIA’S SECRET
2018/04/02 全球购物
介绍一下linux的文件系统
2012/03/20 面试题
js实现弹框效果
2021/03/24 Javascript
机械设计制造专业个人求职信
2013/09/25 职场文书
素质拓展训练感想
2015/08/07 职场文书
协议书格式模板
2016/03/24 职场文书
python3 实现mysql数据库连接池的示例代码
2021/04/17 Python
python库Tsmoothie模块数据平滑化异常点抓取
2022/06/10 Python
JavaScript中10个Reduce常用场景技巧
2022/06/21 Javascript