From: "Eregon (Benoit Daloze)" Date: 2022-05-11T20:29:01+00:00 Subject: [ruby-core:108518] [Ruby master Feature#18774] Add Queue#pop(timeout:) Issue #18774 has been updated by Eregon (Benoit Daloze). Indeed, I forgot to mention that. I think returning `nil` is easiest, most consistent and convenient. I believe it is already common practice to use another element than `nil` in a Queue for special markers, so it seems a rare issue in practice. > however pop already returns nil when called in "non_block" mode Actually it's `queue empty (ThreadError)` in that case. Which seems pretty expensive for such a poll case, but that's the current semantics. When the queue is closed, then `queue.pop` returns `nil` indeed. FWIW the semantics of Rubinius' `Channel#receive_timeout` is to return `nil` when timed out: https://github.com/rubinius/rubinius/blob/b7a755c83f3dd3f0c1f5e546f0e58fb61851ea44/machine/class/channel.cpp#L96 ---------------------------------------- Feature #18774: Add Queue#pop(timeout:) https://bugs.ruby-lang.org/issues/18774#change-97559 * Author: Eregon (Benoit Daloze) * Status: Open * Priority: Normal ---------------------------------------- This has been mentioned many times but somehow was never added. It is useful for many different use cases: * Implementing Timeout#timeout without needing to create a Thread per call which is very inefficient (especially when the timeout is not hit): https://github.com/ruby/timeout/pull/14#issuecomment-1123380880 * @jeremyevans0 I would love a Queue#pop :timeout argument. It would simplify the mutex/condition variable approach currently used for Sequel's connection pool. * @byroot Same. I wanted it so many times * https://bugs.ruby-lang.org/issues/17363 * https://spin.atomicobject.com/2014/07/07/ruby-queue-pop-timeout/ + https://spin.atomicobject.com/2017/06/28/queue-pop-with-timeout-fixed/ * More in my email searches but this seems already plenty I think it should be a keyword argument for clarity, and so there is no confusion with the existing optional argument `non_block=false`. -- https://bugs.ruby-lang.org/ Unsubscribe: