异步编程基础知识

异步在IO密集型场景有优势,应用在爬虫场景就是:

 

import aiohttp  # 异步的库
import asyncio
import time


async def fetch(base_url):
    # 获取一个目标网站的三个页面
    url_list = [base_url + "?o=1", base_url + "?a=22", base_url + "?b=333"]

    for url in url_list:
        print(url)
        async with aiohttp.ClientSession() as session:



        await session.get(url) #异步语句,关键字await产生了阻塞, 

if __name__ == '__main__': time1 = time.time() a = fetch("http://127.0.0.1:8000/aaaa") b = fetch("http://127.0.0.1:8000/bbbb") c = fetch("http://127.0.0.1:8000/ccc") loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait([a, b, c])) print("done:{}".format(time.time() - time1))

 

posted @ 2018-02-21 18:42  炎之朱雀  阅读(108)  评论(0编辑  收藏  举报