asyncio 和aiohttp

# coding=utf8
"""
author:dengjiyun
"""
import asyncio
import aiohttp
import time

start = time.time()

async def get(url):
    
    session = aiohttp.ClientSession()
    
    response = await session.get(url)
    
    result = await response.text()
    
    session.close()
    
    return result


async def request():
    url = 'http://www.baidu.com'
    
    print('Waiting for', url)
    
    result = await get(url)
    
    print('Get response from', url, 'Result:', result)


tasks = [asyncio.ensure_future(request()) for _ in range(5)]

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

end = time.time()
print('Cost time:', end - start)

猜你喜欢

转载自www.cnblogs.com/knighterrant/p/10923274.html