Is it possible to have a fair semaphore in python, one that guarantees that blocking threads are unblocked in the order they call acquire()?
Is it possible to have a fair semaphore in python, one that guarantees that
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You might have to build one from other moving parts. For example, create a
Queue.Queue()to which each listener posts a brand-newEvent()on which it then waits. When it is time to wake up one of the waiting threads, pop off the item on the queue that has been waiting longest — it will be one of those event objects — and release the thread throughevent.set().Obviously, you could also use a semaphore per waiting process, but I like the semantics of an
Eventsince it can clearly only happen once, while a semaphore has the semantics that its value could support many waiting threads.To set the system up:
Then, to wait:
And to release one of the waiting threads:
I suppose the weakness of this approach is that the thread doing the set/release has to wait for a waiting thread to come along, whereas a true semaphore would let several releases proceed even if no one was waiting yet?