初步接触异步,简单地访问一张图片,异步启动的方式是规定的 ,只需要按规范书写就可以了,
import aiohttp, asyncio
async def get_content(link): # 传入的是图片链接
async with aiohttp.ClientSession() as session:
async with session.get(link) as response:
content = await response.read()
print(content)
# return content
url = 'http://www.jj20.com/bz/hhzw/xhtx/75387.html'
loop = asyncio.get_event_loop()
tasks = []
for i in range(100):
tasks.append(asyncio.ensure_future(get_content(url)))
loop.run_until_complete(asyncio.wait(tasks))