首页 > 编程 > Python > 正文

Python多线程处理实例详解【单进程/多进程】

2020-02-16 00:59:58
字体:
来源:转载
供稿:网友

本文实例讲述了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#            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表