进程池callback

from multiprocessing import Pool
import requests
import json
import os

def get_page(url):
    print('%s get %s'%(os.getpid(),url))
    res=requests.get(url)
    if res.status_code == 200:
        return {'url':url,'text':res.text}     #这个字典直接传给callback函数处理

def parse_page(res):
    print('%s parse %s'%(os.getpid(),res['url']))
    parse_res='url:%s size:%s\n'%(res['url'],len(res['text']))
    with open('db.txt','a+') as f:
        f.write(parse_res)
if __name__=='__main__':
    urls = [
        'https://www.baidu.com',
        'https://www.python.org',
        'https://www.openstack.org',
        'https://help.github.com/',
        'http://www.sina.com.cn/'
    ]
    p=Pool(3)
    res_list=[]
    for url in urls:
        res=p.apply_async(get_page,args=(url,),callback=parse_page) #res拿到的是get_page的结果,
        res_list.append(res)
    p.close()
    p.join()
    print(res_list)

    # for res in res_list:
    #     print(res.get()) 打印字典

4200 get https://www.baidu.com
9392 get https://www.python.org
11912 get https://www.openstack.org
4200 get https://help.github.com/
11828 parse https://www.baidu.com
11912 get http://www.sina.com.cn/
11828 parse https://www.openstack.org
11828 parse https://www.python.org
11828 parse https://help.github.com/
11828 parse http://www.sina.com.cn/
[<multiprocessing.pool.ApplyResult object at 0x02B37330>, <multiprocessing.pool.ApplyResult object at 0x02B37390>, <multiprocessing.pool.ApplyResult object at 0x02B373F0>, <multiprocessing.pool.ApplyResult object at 0x02B37450>, <multiprocessing.pool.ApplyResult object at 0x02B374B0>]
<multiprocessing.pool.ApplyResult object at 0x02B374B0>


Process finished with exit code 0



 

posted @ 2018-11-20 17:29  986428528  阅读(127)  评论(0编辑  收藏  举报