Created
July 5, 2021 14:08
-
-
Save discort/4c697985e2ad8dc6455895ae4b203335 to your computer and use it in GitHub Desktop.
Fetching via queue
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 asyncio | |
import aiohttp | |
async def fetch(session, url): | |
async with session.get(url) as response: | |
return await response.text() | |
async def consumer(queue, session): | |
while True: | |
url = await queue.get() | |
if url is None: | |
break | |
try: | |
return await fetch(session, url) | |
except aiohttp.ClientError as e: | |
print(e) | |
queue.task_done() | |
async def main(): | |
queue = asyncio.Queue() | |
urls = [ | |
'http://python.org', | |
'https://google.com', | |
'http://yifei.me' | |
] | |
for url in urls: | |
queue.put_nowait(url) | |
queue.put_nowait(None) | |
async with aiohttp.ClientSession() as session: | |
tasks = [consumer(queue, session) for _ in range(3)] | |
htmls = await asyncio.gather(*tasks, return_exceptions=True) | |
for html in htmls: | |
print(html[:100]) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment