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爬虫教程之爬取百度贴吧并下载的示例
Mar 07 Python
python网络编程示例(客户端与服务端)
Apr 24 Python
Python3实现从文件中读取指定行的方法
May 22 Python
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
Dec 25 Python
通过5个知识点轻松搞定Python的作用域
Sep 09 Python
通过python+selenium3实现浏览器刷简书文章阅读量
Dec 26 Python
python使用opencv驱动摄像头的方法
Aug 03 Python
python机器学习之KNN分类算法
Aug 29 Python
python程序中的线程操作 concurrent模块使用详解
Sep 23 Python
配置python的编程环境之Anaconda + VSCode的教程
Mar 29 Python
Matplotlib 折线图plot()所有用法详解
Jul 28 Python
Python破解极验滑动验证码详细步骤
May 21 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通过array_merge()函数合并关联和非关联数组的方法
2015/03/18 PHP
使用PHP+MySql实现微信投票功能实例代码
2017/09/29 PHP
数据结构之利用PHP实现二分搜索树
2020/10/25 PHP
浅谈JavaScript中面向对象技术的模拟
2006/09/25 Javascript
JavaScript动态操作表格实例(添加,删除行,列及单元格)
2013/11/25 Javascript
jquery通过a标签删除table中的一行的代码
2013/12/02 Javascript
javascript中setTimeout使用指南
2015/07/26 Javascript
分享一些常用的jQuery动画事件和动画函数
2015/11/27 Javascript
学习AngularJs:Directive指令用法(完整版)
2016/04/26 Javascript
jQuery仿京东商城楼梯式导航定位菜单
2016/07/25 Javascript
AngularJS通过$http和服务器通信详解
2016/09/21 Javascript
老生常谈JavaScript中的this关键字
2016/10/01 Javascript
JavaScript 中 apply 、call 的详解
2017/03/21 Javascript
js 发布订阅模式的实例讲解
2017/09/10 Javascript
JS库之ParticlesJS使用简介
2017/09/12 Javascript
BootStrap 标题设置跨行无效的解决方法
2017/10/25 Javascript
nodejs+mongodb+vue前后台配置ueditor的示例代码
2018/01/02 NodeJs
浅谈JavaScript面向对象--继承
2019/03/20 Javascript
VueJS 取得 URL 参数值的方法
2019/07/19 Javascript
JavaScript实现好看的跟随彩色气泡效果
2020/02/06 Javascript
Python实现读取SQLServer数据并插入到MongoDB数据库的方法示例
2018/06/09 Python
Python代码打开本地.mp4格式文件的方法
2019/01/03 Python
python 判断矩阵中每行非零个数的方法
2019/01/26 Python
详解Python下载图片并保存本地的两种方式
2019/05/15 Python
python多线程并发实例及其优化
2019/06/27 Python
Python 计算任意两向量之间的夹角方法
2019/07/05 Python
python字典的setdefault的巧妙用法
2019/08/07 Python
Pytorch 实现自定义参数层的例子
2019/08/17 Python
Python操作qml对象过程详解
2019/09/26 Python
爬虫代理池Python3WebSpider源代码测试过程解析
2019/12/20 Python
tensorflow使用指定gpu的方法
2020/02/04 Python
Keras 在fit_generator训练方式中加入图像random_crop操作
2020/07/03 Python
PyTorch 中的傅里叶卷积实现示例
2020/12/11 Python
联想中国官方商城:Lenovo China
2017/10/18 全球购物
2015年度招聘工作总结
2015/05/28 职场文书
工作汇报材料难写?方法都在这里了!
2019/07/01 职场文书