Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created January 19, 2023 22:16
Show Gist options
  • Save sheenobu/11287f22dceeb2fa734905b802706c17 to your computer and use it in GitHub Desktop.
Save sheenobu/11287f22dceeb2fa734905b802706c17 to your computer and use it in GitHub Desktop.
aiohttp_example
import asyncio
import aiohttp
import logging
async def fetchurl(session, url: str):
async with session.get(url) as response:
return len(await response.text())
async def urlfetcher(session, queue):
items = []
while True:
try:
x = await queue.get()
print(x)
items.append(await fetchurl(session, x))
queue.task_done()
except asyncio.exceptions.CancelledError as e:
return items
async def run():
# simple
l = ["https://google.com" for x in [1,2,3,4,5,6]]
async with aiohttp.ClientSession() as session:
tasks = [fetchurl(session, url) for url in l]
lx = await asyncio.gather(*tasks)
print(lx)
# worker
queue = asyncio.Queue()
for x in l:
queue.put_nowait(x)
tasks = []
async with aiohttp.ClientSession() as session:
for i in range(3):
task = asyncio.create_task(urlfetcher(session, queue))
tasks.append(task)
await queue.join()
for task in tasks:
task.cancel()
l = await asyncio.gather(*tasks)
print(l)
print(asyncio.run(run()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment