Skip to content

Allow CapacityLimiter to have total_tokens=0 #3243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Zac-HD opened this issue Apr 4, 2025 · 1 comment
Open

Allow CapacityLimiter to have total_tokens=0 #3243

Zac-HD opened this issue Apr 4, 2025 · 1 comment

Comments

@Zac-HD
Copy link
Member

Zac-HD commented Apr 4, 2025

Sometimes, there are use-cases where you want to entirely pause processing of some tasks, and then later scale back up. CapacityLimiter is basically perfect for this kind of thing, except that it currently raises an error if you pass total_tokens=0:

trio/src/trio/_sync.py

Lines 254 to 255 in d988edd

if new_total_tokens < 1:
raise ValueError("total_tokens must be >= 1")

It's already possible to get the desired effect just by deliberately "leaking" one of the tokens, but it would be much nicer to simply allow zero as the intended capacity, by editing the numbers in the setter above.

@TeamSpen210
Copy link
Contributor

Might need to make sure this is tested too, since zero is rather susceptible to edge cases. Looks safe though.

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

No branches or pull requests

2 participants