You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to use Trio for a simple scraping project. Let's say I have a million URLs, but I only have the resources on my box to handle 15 connections at a time. Is there a way to nursery to hold off on executing the rest of the jobs until the current 15 being executed have finished?
Nurseries don't have this built in, no. Two ways that come to mind:
start 15 tasks, and have them each loop, pulling items off your list of 1,000,000 urls.
start 1,000,000 tasks, and have them all use a CapacityLimiter so that only 15 are actually making a request at any given time.
The second one unfortunately will use a lot more memory, because tasks in trio are pretty cheap but still do take some memory, more than a simpler string sitting in a list.
If you want to get fancier, it would also be possible to implement a reusable capacity-limited nursery class... We should think about adding that to docs as an example, or something. (I guess the semantic subtlety would be what to do with the queued tasks if there's an unhandled error – normally trio guarantees that after you call start_soon the task will run at least until the first await, even if it's cancelled, which is a useful guarantee because it gives with blocks and such a chance to run and clean up resources that might have been passed into the task. But then, maybe if you're using a special object whose whole point is to avoid allocating too many resources at once, though, then you shouldn't be allocating resources until the task actually starts.)
I'm doing a performance benchmark of a web service, I want to measure the rate at which it accepts messages under sustained load.
I tried the second solution with the CapacityLimiter using the "asks" library but I easily run out of file descriptors (sockets, most likely) and the script crashes with errno 24: too many open files unless I set a really low limit like 4. (ulimit -n returns 1024 but even a limit of 8 is enough to cause the issue frequently)
Maybe I'm doing something wrong, my code is more or less this:
Running out of sockets with a limit of 4 seems weird... Maybe you're keeping the socket open past the end of the CapacityLimiter block? Can you close the response object or something?
I think you're correct that the sockets are being kept open for longer than they should, but there seems to be no way to explicitly close them. They are probably closed/cleaned up automatically after each send_message terminates but not quickly enough to not cause problems.
I was able to solve it by using an asks Session with a few hundred connections, that way I don't even need to use a CapacityLimiter and I don't run out of file descriptors/sockets, so thanks for the link! :) (I just started experimenting with trio and asks yesterday, but so far I'm absolutely loving it!)
For anyone that ends up here with the same problem, this is what I did:
This is on asks' end, and a fix will be pushed this evening :)
@njsmith was correct. The base methods each create a Session. Currently the code relies on python to clean up the sockets, but evidently this may not be fast enough. I'll start force closing them. Using the Session is the correct way to go anyway, and doesn't have this issue :D Thanks for the feedback guys.
The following seems to work, but I would appreciate any confirmation: I use a CapacityLimiter in the task function, and start it in the nursery with nursery.start instead of nursery.start_soon. Moreover, the .started() method is called within the async with my_capacity_limiter: in the task function.
This way, I want to use the capacity limiter for both, limiting the number of concurrent tasks as well as the size of the nursery. Is this effective?
Nurseries don't have this built in, no. Two ways that come to mind:
start 15 tasks, and have them each loop, pulling items off your list of 1,000,000 urls.
start 1,000,000 tasks, and have them all use a CapacityLimiter so that only 15 are actually making a request at any given time.
The second one unfortunately will use a lot more memory, because tasks in trio are pretty cheap but still do take some memory, more than a simpler string sitting in a list.
If you want to get fancier, it would also be possible to implement a reusable capacity-limited nursery class... We should think about adding that to docs as an example, or something. (I guess the semantic subtlety would be what to do with the queued tasks if there's an unhandled error – normally trio guarantees that after you call start_soon the task will run at least until the first await, even if it's cancelled, which is a useful guarantee because it gives with blocks and such a chance to run and clean up resources that might have been passed into the task. But then, maybe if you're using a special object whose whole point is to avoid allocating too many resources at once, though, then you shouldn't be allocating resources until the task actually starts.)
Maybe a third way,
acquire from a semaphore when launching tasks
release to the semaphore, from the task, when it ends
=> if the parallelism is way more than needed, only the needed tasks are started
Figured out a nice way to handle this, and then noticed that @bronger has actually mentioned this method already. Just sharing an example code here, since it's nice that it allows us to use the limiter's async context manager rather than manually acquiring and releasing it.
importtriofromtrioimportCapacityLimiterasyncdefsomething(
limiter: trio.CapacityLimiter,
index: int,
task_status: trio.TaskStatus[None] =trio.TASK_STATUS_IGNORED,
):
print(f"Task {index+1} has been started")
asyncwithlimiter:
task_status.started()
awaittrio.sleep(5)
print(f"Task {index+1} has finished")
asyncdefmain():
limiter=CapacityLimiter(10)
asyncwithtrio.open_nursery() astg:
forindexinrange(20):
awaittg.start(something, limiter, index)
trio.run(main)
Activity
njsmith commentedon May 10, 2018
Nurseries don't have this built in, no. Two ways that come to mind:
start 15 tasks, and have them each loop, pulling items off your list of 1,000,000 urls.
start 1,000,000 tasks, and have them all use a
CapacityLimiterso that only 15 are actually making a request at any given time.The second one unfortunately will use a lot more memory, because tasks in trio are pretty cheap but still do take some memory, more than a simpler string sitting in a list.
If you want to get fancier, it would also be possible to implement a reusable capacity-limited nursery class... We should think about adding that to docs as an example, or something. (I guess the semantic subtlety would be what to do with the queued tasks if there's an unhandled error – normally trio guarantees that after you call
start_soonthe task will run at least until the firstawait, even if it's cancelled, which is a useful guarantee because it giveswithblocks and such a chance to run and clean up resources that might have been passed into the task. But then, maybe if you're using a special object whose whole point is to avoid allocating too many resources at once, though, then you shouldn't be allocating resources until the task actually starts.)ric2b commentedon May 16, 2018
I was also wondering about this.
I'm doing a performance benchmark of a web service, I want to measure the rate at which it accepts messages under sustained load.
I tried the second solution with the CapacityLimiter using the "asks" library but I easily run out of file descriptors (sockets, most likely) and the script crashes with
errno 24: too many open filesunless I set a really low limit like 4. (ulimit -nreturns 1024 but even a limit of 8 is enough to cause the issue frequently)Maybe I'm doing something wrong, my code is more or less this:
Because I want to make sure that the script isn't a bottleneck, I'd like to have a much larger number of concurrent connections, like a few hundreds.
Using the CapacityLimiter seems like the cleaner solution, so I'd prefer to keep it instead of adding a task queue, any ideas?
njsmith commentedon May 16, 2018
Running out of sockets with a limit of 4 seems weird... Maybe you're keeping the socket open past the end of the
CapacityLimiterblock? Can you close the response object or something?Another thing to watch out for is: https://asks.readthedocs.io/en/latest/a-look-at-sessions.html#important-connection-un-limiting
But I think that only matters if you're using an explicit
Sessionobject, which this code isn't.ric2b commentedon May 16, 2018
I think you're correct that the sockets are being kept open for longer than they should, but there seems to be no way to explicitly close them. They are probably closed/cleaned up automatically after each
send_messageterminates but not quickly enough to not cause problems.I was able to solve it by using an asks Session with a few hundred connections, that way I don't even need to use a CapacityLimiter and I don't run out of file descriptors/sockets, so thanks for the link! :) (I just started experimenting with trio and asks yesterday, but so far I'm absolutely loving it!)
For anyone that ends up here with the same problem, this is what I did:
theelous3 commentedon May 17, 2018
This is on asks' end, and a fix will be pushed this evening :)
@njsmith was correct. The base methods each create a Session. Currently the code relies on python to clean up the sockets, but evidently this may not be fast enough. I'll start force closing them. Using the Session is the correct way to go anyway, and doesn't have this issue :D Thanks for the feedback guys.
ric2b commentedon May 17, 2018
That's great!
I'm really loving the trio + asks combination, it makes large amounts of concurrent http requests so effortless and readable!
Thanks to everyone contributing to them :) (hopefully I will too, eventually)
bronger commentedon Dec 31, 2023
The following seems to work, but I would appreciate any confirmation: I use a
CapacityLimiterin the task function, and start it in the nursery withnursery.startinstead ofnursery.start_soon. Moreover, the.started()method is called within theasync with my_capacity_limiter:in the task function.This way, I want to use the capacity limiter for both, limiting the number of concurrent tasks as well as the size of the nursery. Is this effective?
sebvey commentedon May 6, 2025
Maybe a third way,
=> if the parallelism is way more than needed, only the needed tasks are started
soufianeamini commentedon Aug 26, 2026
Figured out a nice way to handle this, and then noticed that @bronger has actually mentioned this method already. Just sharing an example code here, since it's nice that it allows us to use the limiter's async context manager rather than manually acquiring and releasing it.