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
Once a friend of mine explained me what a semaphore is really nicely. He said that it's like a sofa, at most n people can sit on it at a time and others need to wait for free seats. I would have probably never understood what semaphores are without this explanation.
Trio already has nurseries instead of task groups, so I think it would make sense to have sofas instead of semaphores.
Semaphores do have practical usage patterns that don't fall into the Sofa pattern -- for example, trio.Queue uses a Semaphore whose value starts at zero and then put releases and get acquires, so it tracks how many items are available in the Queue's buffer.
But maybe it would make sense to have Sofa be a more specialized and friendly Semaphore, that enforces that a task can't release unless it has already acquired? (trio.Lock already departs from threading.Lock in this regard... a threading.Lock can be released by any thread, even one that didn't acquire it. trio.Lock is pickier.)
Sounds good to me. Sofa probably shouldn't inherit from Semaphore because it should have user-friendly method names, like take_a_seat instead of acquire.
I guess a more boring but possibly also more discoverable name for such a primitive would be CapacityLock or similar. [Edit: though, on further thought, eh. The metaphor behind "lock" is that it lets you keep other people (tasks) out, and enjoy exclusive access to some resource. But using a semaphore like this is useless in the cases where you need exclusivity; the usual case is where every task is using some separate object, but those objects are expensive so you want to limit how many are in use. "Lock" really doesn't communicate this at all.]
We could also rename Lock to Chair, but I think that would be overkill. People can't really guess what a chair would do and locks aren't hard to understand.
Another argument for having a dedicated class for this is that if we enforce the rule that only the task that acquired the sofa-seat could release it, then we could use that information later to automatically detect bugs like a task that exits without giving up its seat, or even potentially do deadlock detection. (Deadlock detection relies on reasoning like: task 1 can't make progress until lock A is released, and lock A can only be released by task 2, and task 2 can't make progress until lock B is released, but lock B can only be released by task 1, therefore we have a bug. With traditional semaphores and Python's threading.Lock, the "lock X can only be released by task Y" steps don't work, so you can't really do deadlock detection AFAICT.)
However, I'm still not sure that Sofa is a better name than CapacityLimit. I love the Sofa name but I feel like there's a risk it'd create a barrier for newcomers and if so that would have to take priority...
Currently @theelous3's asks docs say that "a semaphore is like a sofa". Wouldn't it be nicer to just have a Sofa to begin with? Like I said earlier, we already have nuresies :)
Yeah... sadly, I think a project like this has a non-zero but very limited budget for whimsical naming, and I eventually decided we should reserve it for more central and unusual concepts like nurseries. If I'm looking for a way to limit how many things are happening at once to avoid overloading things, then maybe I won't look for CapacityLimiter directly but I'll probably notice it if I'm skimming through the API docs, while Sofa is not so discoverable.
(But you should totally feel free to from trio import CapacityLimiter as Sofa. We know what it really is.)
Activity
njsmith commentedon May 13, 2017
Ha, that's great!
Semaphores do have practical usage patterns that don't fall into the Sofa pattern -- for example, trio.Queue uses a Semaphore whose value starts at zero and then
putreleases andgetacquires, so it tracks how many items are available in the Queue's buffer.But maybe it would make sense to have Sofa be a more specialized and friendly Semaphore, that enforces that a task can't release unless it has already acquired? (
trio.Lockalready departs fromthreading.Lockin this regard... athreading.Lockcan be released by any thread, even one that didn't acquire it.trio.Lockis pickier.)Akuli commentedon May 13, 2017
Sounds good to me. Sofa probably shouldn't inherit from Semaphore because it should have user-friendly method names, like
take_a_seatinstead ofacquire.njsmith commentedon May 13, 2017
I guess a more boring but possibly also more discoverable name for such a primitive would be
CapacityLockor similar. [Edit: though, on further thought, eh. The metaphor behind "lock" is that it lets you keep other people (tasks) out, and enjoy exclusive access to some resource. But using a semaphore like this is useless in the cases where you need exclusivity; the usual case is where every task is using some separate object, but those objects are expensive so you want to limit how many are in use. "Lock" really doesn't communicate this at all.]theelous3 commentedon May 13, 2017
Who says sofa's are immutable: https://www.youtube.com/watch?v=_zfni9tetRM
Akuli commentedon May 14, 2017
We could also rename Lock to Chair, but I think that would be overkill. People can't really guess what a chair would do and locks aren't hard to understand.
njsmith commentedon May 29, 2017
Another argument for having a dedicated class for this is that if we enforce the rule that only the task that acquired the sofa-seat could release it, then we could use that information later to automatically detect bugs like a task that exits without giving up its seat, or even potentially do deadlock detection. (Deadlock detection relies on reasoning like: task 1 can't make progress until lock A is released, and lock A can only be released by task 2, and task 2 can't make progress until lock B is released, but lock B can only be released by task 1, therefore we have a bug. With traditional semaphores and Python's
threading.Lock, the "lock X can only be released by task Y" steps don't work, so you can't really do deadlock detection AFAICT.)However, I'm still not sure that
Sofais a better name thanCapacityLimit. I love theSofaname but I feel like there's a risk it'd create a barrier for newcomers and if so that would have to take priority...Akuli commentedon Jun 2, 2017
Currently @theelous3's asks docs say that "a semaphore is like a sofa". Wouldn't it be nicer to just have a Sofa to begin with? Like I said earlier, we already have nuresies :)
Implement capacity limitation for threads
Implement capacity limitation for threads
njsmith commentedon Aug 19, 2017
Yeah... sadly, I think a project like this has a non-zero but very limited budget for whimsical naming, and I eventually decided we should reserve it for more central and unusual concepts like nurseries. If I'm looking for a way to limit how many things are happening at once to avoid overloading things, then maybe I won't look for
CapacityLimiterdirectly but I'll probably notice it if I'm skimming through the API docs, whileSofais not so discoverable.(But you should totally feel free to
from trio import CapacityLimiter as Sofa. We know what it really is.)