Skip to content

Instantly share code, notes, and snippets.

@cgarciae
Last active May 14, 2022 23:00
Show Gist options
  • Save cgarciae/04f17402a186dd948473515703c20228 to your computer and use it in GitHub Desktop.
Save cgarciae/04f17402a186dd948473515703c20228 to your computer and use it in GitHub Desktop.
client-task-pool.py
# client-task-pool.py
from aiohttp import ClientSession, TCPConnector
import asyncio
import sys
from pypeln.task import TaskPool
limit = 1000
async def fetch(url, session):
async with session.get(url) as response:
return await response.read()
async def _main(url, total_requests):
connector = TCPConnector(limit=None)
async with ClientSession(connector=connector) as session, TaskPool.create(limit) as tasks:
for i in range(total_requests):
await tasks.put(fetch(url.format(i), session))
url = "http://localhost:8080/{}"
loop = asyncio.get_event_loop()
loop.run_until_complete(_main(url, int(sys.argv[1])))
@mengyyy
Copy link

mengyyy commented Dec 23, 2018

async with ClientSession(connector=connector) as session, TaskPool(limit) as tasks:

change to

async with ClientSession(connector=connector) as session, TaskPool(limit, loop=loop) as tasks:

@chitzinwin
Copy link

chitzinwin commented Dec 8, 2020

TypeError: init() got an unexpected keyword argument 'loop', when passing event loop to Taskpool

@cgarciae
Copy link
Author

cgarciae commented Dec 8, 2020

Updated gist, TaskPool is now a dataclass that has a create constructor:
https://github.com/cgarciae/pypeln/blob/5909376b30fe25fd869e49e4e46b7782d48f1be2/pypeln/task/worker.py#L193

@cgarciae
Copy link
Author

cgarciae commented Dec 8, 2020

@chanpu9
Copy link

chanpu9 commented Sep 17, 2021

@cgarciae I found the error messages when I ran this script:
TypeError: 'coroutine' object is not callable
sys:1: RuntimeWarning: coroutine 'fetch' was never awaited
image

@chanpu9
Copy link

chanpu9 commented Sep 17, 2021

I can run this script but I would like to ask you @cgarciae something. How should I write the code to handler the error and kill the remained task?

@brigxt0
Copy link

brigxt0 commented May 14, 2022

I can run this script but I would like to ask you @cgarciae something. How should I write the code to handler the error and kill the remained task?

l know its been ages but how did you manage to get it to run past the TypeError? l'm experiencing the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment