• python多进程简单示例

    发布时间:2019-11-27 23:06:05
    作者:ynkulusi
  • 多进程例子,参考学习用
    #!/usr/bin/env python                                                                                                                               
    from multiprocessing import Process                                                                                                                 
    from os import getpid                                                                                                                               
    from random import randint                                                                                                                          
    from time import time, sleep                                                                                                                        
                                                                                                                                                        
    def download_task(filename):                                                                                                                        
        print('---启动下载进程,进程号[%d].' % getpid())                                                                                                
        print('###开始下载[ %s ... ]' % filename)                                                                                                       
        time_to_download = randint(3, 10)                                                                                                               
        sleep(time_to_download)                                                                                                                         
        print('***[ %s ]下载完成! 耗费了%d秒' % (filename, time_to_download))                                                                           
                                                                                                                                                        
    def main():                                                                                                                                         
        start = time()                                                                                                                                  
        p1 = Process(target=download_task, args=('MySQL从删库到跑路.pdf', ))                                                                            
        p1.start()                                                                                                                                      
        p2 = Process(target=download_task, args=('万万没想到.mp4', ), daemon=True)   #设置守护进程,主进程终止时子进程跟随终止.                          
        p2.start()                                                                                                                                      
        p1.join()   #设置阻塞主进程,直到该子进程终止或超时才回到主进程执行,超时设置join(3),单位是秒.                                                    
        #p2.join()   #join()和daemon=True用意相反,但若同时设置,则可让该子进程正常运行终止后回到主进程执行.                                              
        end = time()                                                                                                                                    
        print('总共耗费了%.2f秒.' % (end - start))                                                                                                      
                                                                                                                                                        
    if __name__ == '__main__':                                                                                                                          
        main()
     
  • 分类:python
    标签: multiprocessing
    评论数:2 阅读数:824