Process

作者: 錦魚 | 来源:发表于2018-11-27 19:37 被阅读0次
    • 队列 . manager 传递数据
    • 两个进程池

    进程

    • 进程资源不共享,manage()方法可以通信
    • 进程消耗CPU资源

    进程间资源不共享--- 进程间的通信
    import os
    from multiprocessing import Queue,Process
    
    def write_data(data_queue):
        for i in range(30):
            data_queue.put(i)
        print(os.getpid())
    
    def read_data(data_queue):
        while not data_queue.empty():
            print(data_queue.get())
        print(os.getpid())
    
    if __name__ == '__main__':
        q = Queue()
         # group: Any   分组
         # target: Optional[Callable]进程要处理的任务
         # name: Optional[str]  进程名
         # args: Iterable[Any]  参数
         # kwargs: Mapping[Any, Any] = ...,参数包
         # daemon: Optional[bool] = ...) -> None: ...    
    
        process1 = Process(target=write_data,name='p1',args=(q,))
        process1.start()
        process1.join()
    
        process2 = Process(target=read_data,name='p2',args=(q,))
        process2.start()
        process2.join()
    
    实现共享进程间资源共享--- 进程间的通信
    import os,time
    from multiprocessing import Pool,Manager
    
    def write_data(data_queue,num):
        for inf in range(num):
            data_queue.put(inf)
        print('write_over',os.getpid())
    
    def read_data(data_queue):
        while not data_queue.empty():
            a = data_queue.get()
            print(a)
        print('read_over')
    
    if __name__ == '__main__':
        q = Manager().Queue()
        #创建进程池
        pool = Pool(4)
        pool.apply_async(write_data,(q,40))
    
        time.sleep(2)
        start = time.time()
        pool.apply_async(read_data,(q,))
        #关闭
        pool.close()
        end = time.time()
        print(end-start)
    

    进程池(1)
    from multiprocessing import Pool
    from fake_useragent import UserAgent
    import requests
    
    
    def download_image(imageUrl,imageName):
        print(imageUrl,imageName)
        req_header={'User-Agent':UserAgent().random}
        response = requests.get(url=imageUrl,headers=req_header)
        if response.status_code==200:
            return response.status_code,imageName,response.content
    
    def download_done(future):
        print('执行了回调方法')
        print(future)
        imageName = future[1]
        with open(imageName,'wb') as file:
            file.write(future[2])
    
    if __name__ == '__main__':
        pool = Pool(4)
        for i in range(40):
            imageurl = 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2811473792,2413507956&fm=11&gp=0.jpg'
            pool.apply_async(func=download_image,args=(imageurl,'img{}.jpg'.format(i)),callback=download_done)
        pool.close()
        pool.join()
    
    进程池(2)python自带的
    from concurrent.futures import ProcessPoolExecutor
    from fake_useragent import UserAgent
    import requests
    
    def download_image(imgUrl,imgName):
        print(imgUrl,imgName)
        req_header = {'UserAgent':UserAgent().random}
        response = requests.get(url=imgUrl,headers=req_header)
        if response.status_code==200:
            return response.status_code,imgName,response.content
    def download_done(future):
        print('执行了回调函数')
        print(future.result())
    
    if __name__ == '__main__':
        #创建进程池  #python自带的____容易出现问题
        pool = ProcessPoolExecutor(4)
        for i in range(40,70):
            imgurl= 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2811473792,2413507956&fm=11&gp=0.jpg'
            #往进程池添加任务 fn(函数名称)
            handler = pool.submit(download_image,imgurl,'img{}.jpg'.format(i) )
            #添加回调方法
            handler.add_done_callback(download_done)
        #shutdown里默认有join方法
        pool.shutdown()
        print('over_process')
    

    相关文章

      网友评论

          本文标题:Process

          本文链接:https://www.haomeiwen.com/subject/kkygqqtx.html