Created
May 9, 2021 21:58
-
-
Save gowerc/6d0cdfd94838976c2f8757f3ebfa5d96 to your computer and use it in GitHub Desktop.
Example of using Async http requests in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aiohttp | |
import asyncio | |
URLS = [ | |
"http://www.laughfactory.com/jokes/clean-jokes/2", | |
"http://www.laughfactory.com/jokes/clean-jokes/3", | |
"http://www.laughfactory.com/jokes/clean-jokes/4", | |
"http://www.laughfactory.com/jokes/clean-jokes/5", | |
"http://www.laughfactory.com/jokes/clean-jokes/6", | |
"http://www.laughfactory.com/jokes/clean-jokes/7", | |
"http://www.laughfactory.com/jokes/clean-jokes/8", | |
"http://www.laughfactory.com/jokes/clean-jokes/9", | |
"http://www.laughfactory.com/jokes/clean-jokes/10", | |
"http://www.laughfactory.com/jokes/clean-jokes/11", | |
"http://www.laughfactory.com/jokes/clean-jokes/12", | |
"http://www.laughfactory.com/jokes/clean-jokes/13", | |
] | |
async def get_url(url, session): | |
async with session.get(url = url) as resp: | |
print("Got {url} - {code}".format( | |
url = url, | |
code = resp.status | |
)) | |
assert resp.status == 200 | |
return await resp.text() | |
async def main(): | |
connector = aiohttp.TCPConnector(limit=4) | |
async with aiohttp.ClientSession(connector=connector) as session: | |
results = await asyncio.gather(*[ | |
get_url(url, session) for url in URLS | |
]) | |
return results | |
loop = asyncio.new_event_loop() | |
x = loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment