Skip to content

Idea: rename Semaphore to Sofa? #156

Closed

Description

@Akuli

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.

Activity

  1. njsmith commented on May 13, 2017

    @njsmith
    Member

    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 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.)

  2. Akuli commented on May 13, 2017

    @Akuli
    Author

    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.

  3. njsmith commented on May 13, 2017

    @njsmith
    Member

    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.]

  4. Akuli commented on May 14, 2017

    @Akuli
    Author

    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.

  5. njsmith commented on May 29, 2017

    @njsmith
    Member

    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...

  6. Akuli commented on Jun 2, 2017

    @Akuli
    Author

    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 :)

  7. njsmith commented on Aug 19, 2017

    @njsmith
    Member

    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.)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions