Python Aiohttp:关于Session对象的实用程序

时间:2017-08-20 12:42:20

标签: python aiohttp

以下是从交互式经纪人网站获取链接的1个工作代码。

在aiohttp的documentation中,他们总是使用aiohttp.ClientSession()对象,以便" sessions"从一个请求重用到另一个请求。但是我从多个请求示例(here for instance)中看到的是每个请求创建1个会话...?那么Session对象的兴趣是什么?

import asyncio
from aiohttp import ClientSession

exchanges_by_locs=[]
inst_type_dicts=[]
async def inst_types(url):
    async with ClientSession() as session:
        async with session.get(url) as response:
            response = await response.text()
            html = lxml.html.fromstring(response)
            p=html.xpath('//*[@id="toptabs"]/ul/li')
            for e in p:
                inst=dict(inst_type=e.find('a/span').text, 
                          url='https://www.interactivebrokers.com'+e.find('a').attrib['href'])
                inst_type_dicts.append(inst)

async def inst_by_loc(inst):
    url=inst['url']
    print("start: ",inst['inst_type'])
    async with ClientSession() as session:
        async with session.get(url) as response:
            doc = requests.get(url).content
            html = lxml.html.fromstring(doc)
            p=html.xpath('//*[@class="subtabsmenu"]/li')    
            for e in p:
                exchanges_by_loc=dict(loc=e.find('a/span').text, 
                          loc_url='https://www.interactivebrokers.com'+e.find('a').attrib['href'])
                exchanges_by_locs.append(exchanges_by_loc)
            print("complete: ",inst['inst_type'])

loop = asyncio.get_event_loop()
loop.run_until_complete(inst_types(url))
loop.run_until_complete(
    asyncio.gather(
        *(inst_by_loc(inst) for inst in inst_type_dicts)
    )
)

1 个答案:

答案 0 :(得分:1)

aiohttp的维护者建议尽可能重用会话对象。这是一个很小的表演技巧。