python --- 多线程处理(单进程/多进程)

来源:互联网 发布:一家人颜值都高 知乎 编辑:程序博客网 时间:2024/06/03 14:59

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.py root@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.py root@72132server:~/python/multiprocess# python multprocess.py hello 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.py root@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.py hello 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