python多线程和多进程应用场景(Python多线程处理实例详解单进程/多进程)
类别:脚本大全 浏览量:961
时间:2022-04-03 12:02:03 python多线程和多进程应用场景
Python多线程处理实例详解单进程/多进程本文实例讲述了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)进程查看
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程序设计有所帮助。
原文链接:https://blog.csdn.net/xwbk12/article/details/77623071
您可能感兴趣
- python多进程与多线程详解(Python线程之定位与销毁的实现)
- C# Task实现多线程
- python多线程有两个参数怎么传(python从子线程中获得返回值的方法)
- python线程池有几种(对python 多线程中的守护线程与join的用法详解)
- python 多线程与多进程(python 多线程串行和并行的实例)
- tomcat 多线程并发cpu(Tomcat使用线程池处理远程并发请求的方法)
- ftp上传工具使用方法(CuteFTP多线程FTP上传下载工具功能介绍)
- php怎么实现多线程(PHP实现的多进程控制demo示例)
- python线程池如何实现同步(Python mutiprocessing多线程池pool操作示例)
- python中的多线程详解(python多线程抽象编程模型详解)
- laravel多线程处理请求(Laravel 6 将新增为指定队列任务设置中间件的功能)
- python的多线程比多进程效率高(Python中单线程、多线程和多进程的效率对比实验实例)
- python计算csv的行数(对Python 多线程统计所有csv文件的行数方法详解)
- python多线程多进程运行场景(Python多线程同步---文件读写控制方法)
- python多线程和多进程应用场景(Python多线程处理实例详解单进程/多进程)
- python关闭程序强制退出线程(python多线程调用exit无法退出的解决方法)
- 韩国泡菜做法(韩国泡菜的做法步骤)
- 泰国旅游攻略(泰国旅游攻略必去景点)
- 越难春卷(越难春卷皮怎么用)
- 休闲VS新古典 办公家居简约设计(办公家居简约设计)
- 15个新成 园 位置公布 深圳龙岗2022年共建花园建设又有大动作(15个新成园位置公布)
- 记者手记 书记带我去 巡街(记者手记书记带我去)
热门推荐
- 数据库创建索引的注意事项
- 表空间不足无法登录(System表空间不足的报警问题浅析)
- python 时间戳转化为格式(Python datetime和unix时间戳之间相互转换的讲解)
- Ext.slider控件的用法
- docker镜像内安装python包(如何使用Docker搭建pypi私有仓库)
- Visual Studio使用Git进行源代码管理
- php获取网页内容的几种方法(PHP实现的文件浏览器功能简单示例)
- pycharm 爬虫的数据存在哪了(利用PyCharm Profile分析异步爬虫效率详解)
- react循环有几种方法(React forwardRef的使用方法及注意点)
- mysql新增字段并添加备注(mysql添加备注信息的实现)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9