Python Aiohttp: Regarding utility of the Session object -


here below 1 working piece of code scrape links website of interactive brokers.

in documentation of aiohttp use aiohttp.clientsession() object "sessions" reused 1 requests another. can see multiple requests example (here instance) 1 session created per request...? interest of session object?

import asyncio aiohttp import clientsession  exchanges_by_locs=[] inst_type_dicts=[] async def inst_types(url):     async clientsession() session:         async session.get(url) response:             response = await response.text()             html = lxml.html.fromstring(response)             p=html.xpath('//*[@id="toptabs"]/ul/li')             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 clientsession() session:         async session.get(url) response:             doc = requests.get(url).content             html = lxml.html.fromstring(doc)             p=html.xpath('//*[@class="subtabsmenu"]/li')                 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) inst in inst_type_dicts)     ) ) 

aiohttp's maintainer recommend re-using session object when possible. it's small performance trick.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -