Python多线程处理实例详解【单进程/多进程】
Python  /  管理员 发布于 7年前   308
本文实例讲述了Python多线程处理操作。分享给大家供大家参考,具体如下:
python ― 多线程处理
1、一个进程执行完后,继续下一个进程
root@72132server:~# cd /root/python/multiprocess/root@72132server:~/python/multiprocess# lsmultprocess.pyroot@72132server:~/python/multiprocess# cat multprocess.py#!/usr/bin/python# --*-- coding:utf-8 --*--from multiprocessing import Process,Lock#启用多进程,与进程锁import time,osdef 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# lsmultprocess.pyroot@72132server:~/python/multiprocess# vi multprocess.pyroot@72132server:~/python/multiprocess# ps -ef | grep multiroot 24064 23930 0 20:45 pts/3 00:00:00 grep multiroot@72132server:~/python/multiprocess# ps -ef | grep multiroot 24066 23930 0 20:45 pts/3 00:00:00 grep multiroot@72132server:~/python/multiprocess# ps -ef | grep multiroot 24069 23930 0 20:45 pts/3 00:00:00 grep multiroot@72132server:~/python/multiprocess# ps -ef | grep multiroot 24071 23930 0 20:45 pts/3 00:00:00 grep multiroot@72132server:~/python/multiprocess# ps -ef | grep multiroot 24073 23930 0 20:46 pts/3 00:00:00 grep multiroot@72132server:~/python/multiprocess# ps -ef | grep multiroot 24075 23930 0 20:46 pts/3 00:00:00 grep multiroot@72132server:~/python/multiprocess#
2)脚本运行
root@72132server:~/python/multiprocess# vi multprocess.pyroot@72132server:~/python/multiprocess# python multprocess.pyhello world!!! 0hello world!!! 1hello world!!! 2hello world!!! 3hello world!!! 4hello world!!! 5hello world!!! 6hello world!!! 7hello world!!! 8hello world!!! 9hello world!!! 10hello world!!! 11
2、100个进行同时运行
root@72132server:~/python/multiprocess# lsmultprocess.pyroot@72132server:~/python/multiprocess# cat multprocess.py#!/usr/bin/python# --*-- coding:utf-8 --*--from multiprocessing import Process,Lock#启用多进程,与进程锁import time,osdef 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.pyroot@72132server:~/python/multiprocess# cat multprocess.py#!/usr/bin/python# --*-- coding:utf-8 --*--from multiprocessing import Process,Lock#启用多进程,与进程锁import time,osdef 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.pyhello world!!! 0hello world!!! 2hello world!!! 3hello world!!! 5hello world!!! 7hello world!!! 8hello world!!! 6hello world!!! 9hello world!!! 10hello world!!! 11hello world!!! 14hello world!!! 4hello world!!! 15hello world!!! 16hello world!!! 1hello world!!! 13hello world!!! 18hello world!!! 20hello world!!! 19hello world!!! 21hello world!!! 12hello world!!! 17hello world!!! 23hello world!!! 24hello world!!! 26hello world!!! 27hello world!!! 22hello world!!! 29hello world!!! 31hello world!!! 32hello world!!! 33hello world!!! 34hello world!!! 28hello world!!! 25hello world!!! 30hello world!!! 35hello world!!! 36hello world!!! 39hello world!!! 41hello world!!! 37hello world!!! 40hello world!!! 42hello world!!! 43hello world!!! 46hello world!!! 47hello world!!! 48hello world!!! 38hello world!!! 44hello world!!! 45hello world!!! 50hello world!!! 51hello world!!! 53hello world!!! 54hello world!!! 55hello world!!! 57hello world!!! 49hello world!!! 58hello world!!! 59hello world!!! 60hello world!!! 61hello world!!! 62hello world!!! 63hello world!!! 64hello world!!! 65hello world!!! 66hello world!!! 67hello world!!! 68hello world!!! 69hello world!!! 56hello world!!! 70hello world!!! 52hello world!!! 71hello world!!! 72hello world!!! 73hello world!!! 76hello world!!! 74hello world!!! 78hello world!!! 79hello world!!! 80hello world!!! 82hello world!!! 77hello world!!! 83hello world!!! 84hello world!!! 85hello world!!! 86hello world!!! 87hello world!!! 81hello world!!! 91hello world!!! 75hello world!!! 89hello world!!! 92hello world!!! 88hello world!!! 90hello world!!! 93hello world!!! 95hello world!!! 94hello world!!! 96hello world!!! 98hello world!!! 9
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python进程与线程操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》、《Python+MySQL数据库程序设计入门教程》及《Python常见数据库操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号