码迷,mamicode.com
首页 > 系统相关 > 详细

24 Apr 18 进程

时间:2018-04-24 17:38:24      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:pre   oba   自己的   发送请求   并发   一点   lob   rgs   僵尸   

24 Apr 18
并发的本质:切换+保持状态
一、同一个程序执行多次是多个进程
每一个进程有一个PID
import os
print(‘爹是:‘,os.getppid())    #父的pid (pycharm.exe)
print(‘me是: ‘,os.getpid())     #自己的pid (python.exe)

二、开启子进程的两种方式
windows (createprocess) 创建子进程时子辈除了拷贝父辈的信息还创建了些自己的东西
unix (fork) 创建子进程时拷贝父辈的信息,子进程的初始状态和父辈一致
 
第一种(比较常用)                                                                                                                                                                                                            
from multiprocessing import Process                                                                                                                                                                                  
import time                                                                                                                                                                                                          
                                                                                                                                                                                                                     
def task(name):                                                                                                                                                                                                      
    print(‘%s is running‘ %name)                                                                                                                                                                                     
    time.sleep(3)                                                                                                                                                                                                    
    print(‘%s is done‘ %name)                                                                                                                                                                                        
                                                                                                                                                                                                                     
if __name__ == ‘__main__‘:                                                                                                                                                                                           
    # 在windows系统之上,开启子进程的操作一定要放到这下面                                                                                                                                                                                  
    # Process(target=task,kwargs={‘name‘:‘egon‘})   #两种传参方式皆可                                                                                                                                                         
    p=Process(target=task,args=(‘egon‘,))          #两种传参方式皆可                                                                                                                                                         
    p.start() # 向操作系统发送请求,操作系统会申请内存空间,然后把父进程的数据拷贝给子进程,作为子进程的初始状态                                                                                                                                                     
    print(‘======主‘)                                                                                                                                                                                                 
                                                                                                                                                                                                                     
第二种                                                                                                                                                                                                                  
from multiprocessing import Process                                                                                                                                                                                  
import time                                                                                                                                                                                                          
                                                                                                                                                                                                                     
class MyProcess(Process):                                                                                                                                                                                            
    def __init__(self,name):                                                                                                                                                                                         
        super(MyProcess,self).__init__()     #Process在init里面有相应设置,要遗传下来,否则报错                                                                                                                                         
        self.name=name                                                                                                                                                                                               
                                                                                                                                                                                                                     
    def run(self):                                                                                                                                                                                                   
        print(‘%s is running‘ %self.name)                                                                                                                                                                            
        time.sleep(3)                                                                                                                                                                                                
        print(‘%s is done‘ %self.name)                                                                                                                                                                               
                                                                                                                                                                                                                     
if __name__ == ‘__main__‘:                                                                                                                                                                                           
    p=MyProcess(‘egon‘)                                                                                                                                                                                              
    p.start()   #p.start()调用了类中的run()方法(规定)                                                                                                                                                                          
print(‘主‘) 
 
三、进程的内存空间相互隔离
from multiprocessing import Process                     
import time                                             
                                                        
x=1000                                                  
                                                        
def task():                                             
    time.sleep(3)                                       
    global x                                            
    x=0                                                 
    print(‘儿子死啦‘,x)                                     
 
#在之前最好只有函数或变量的定义,没有具体的执行(print等)                                                        
if __name__ == ‘__main__‘:                                                                       
    p=Process(target=task)                              
    p.start()                                           
    time.sleep(5)                                       
print(x)     
#在子进程中对变量x的修改不影响父进程中x的值
 
四、父进程等待子进程结束 p1.join()
#笨
from multiprocessing import Process                                     
import time                                                             
                                                                        
x=1000                                                                  
                                                                        
def task(n):                                                            
    print(‘%s is runing‘ %n)                                            
    time.sleep(n)                                                       
                                                                        
if __name__ == ‘__main__‘:                                              
    start_time=time.time()                                              
                                                                        
    p1=Process(target=task,args=(1,))                                   
    p2=Process(target=task,args=(2,))                                   
    p3=Process(target=task,args=(3,))                                   
    p1.start()                                                          
    p2.start()                                                          
    p3.start()                                                          
                                                                        
    p3.join() #3s                                                       
    p1.join()                                                           
    p2.join(    )                                                       
                                                                        
print(‘主‘,(time.time() - start_time))        #3.01637601852417      
 
#用循环
from multiprocessing import Process                                   
import time                                                           
                                                                      
x=1000                                                                
                                                                      
def task(n):                                                          
    print(‘%s is runing‘ %n)                                          
    time.sleep(n)                                                     
                                                                      
if __name__ == ‘__main__‘:                                            
    start_time=time.time()                                            
    p_l=[]                                                            
    for i in range(1,4):                                              
        p=Process(target=task,args=(i,))                              
        p_l.append(p)                                                 
        p.start()                                                     
                                                                      
    for p in p_l:                                                     
        p.join()                                                      
                                                                      
print(‘主‘,(time.time() - start_time))       #3.0141923427581787 
 
五、进程对象的其他属性
from multiprocessing import Process                                      
import time                                                              
                                                                         
def task(n):                                                             
    print(‘%s is runing‘ %n)                                             
    time.sleep(n)                                                        
                                                                         
if __name__ == ‘__main__‘:                                               
    start_time=time.time()                                               
    p1=Process(target=task,args=(1,),name=‘任务1‘)                         
    p1.start()                                                           
    print(p1.pid)                                                        
    print(p1.name)     #如前面不定义name,默认process-1 etc                       
    p1.terminate()     #向操作系统发请求,关闭需要一点时间                                
    p1.join()                                                            
    print(p1.is_alive())                                                 
    print(‘主‘)   
 
from multiprocessing import Process                          
import time,os                                               
                                                             
def task():                                                  
    print(‘self:%s parent:%s‘ %(os.getpid(),os.getppid()))   
    time.sleep(3)                                            
                                                             
if __name__ == ‘__main__‘:                                   
    p1=Process(target=task,)                                 
    p1.start()                                               
    print(p1.pid)                                            
print(‘主‘,os.getpid())   
 
六、僵尸进程与孤儿进程
在unix系统中init是所有进程的爹;创建进程用fork,回收进程用waitpid
僵尸进程(有害:占用pid):子代先于父代终结,其部分信息(pid等)没有从系统中删除,需要父代回收。join中含有回收子代信息的功能。
孤儿进程(无害):父代先于子代终结,子代终结后的部分信息由init代收。
 
from multiprocessing import Process                      
import time,os                                           
                                                         
def task(n):                                             
    print(‘%s is running‘ %n)                            
    time.sleep(n)                                        
                                                         
if __name__ == ‘__main__‘:                               
    p1=Process(target=task,args=(1,))                    
    p1.start()                                           
    p1.join()     # join中含有回收子代信息的功能(wait)                                       
    print(‘======主‘,os.getpid())                         
    time.sleep(10000)                                    

                                 

24 Apr 18 进程

标签:pre   oba   自己的   发送请求   并发   一点   lob   rgs   僵尸   

原文地址:https://www.cnblogs.com/zhangyaqian/p/py20180424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!