风-fmgao

导航

异步调用回调实例

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/6/19 14:30
# @File    : 进程池线程池练习.py

from concurrent.futures import ThreadPoolExecutor
import requests
import time


def get(url):  #
    print('GET %s' % url)
    response = requests.get(url)
    # print(response.text)
    time.sleep(3)
    return {'url': url, 'content':response.text}


def parse(res):
    res = res.result()
    print('%s parse res is %s ' % (res['url'], len(res['content'])))
    # print('parse res is %s' % len(res))


if __name__ == '__main__':
    urls = {
        'http://www.cnblogs.com/Linhaifeng',
        'https://www.python.org',
        'https://www.openstack.org',
    }

    pool = ThreadPoolExecutor(2)

    for i in urls:
        pool.submit(get, i).add_done_callback(parse)

posted on 2018-06-19 14:54  风-fmgao  阅读(93)  评论(0编辑  收藏  举报