From: "byroot (Jean Boussier)" <noreply@...>
Date: 2022-05-11T20:43:59+00:00
Subject: [ruby-core:108521] [Ruby master Feature#18774] Add Queue#pop(timeout:)

Issue #18774 has been updated by byroot (Jean Boussier).


> shouldn't we also consider a hypothetical Queue::TimeoutError

IMHO, consistency with closed queues is really more important.

> similar to how all other timeouts work? 

@eregon just beat me to it, but whenever I can I actually use non-exception versions of methods that take a timeout, e.g. `read_nonblock(exception: false)`.

For many the argument is that Exceptions should be for exceptional cases, but IMO the main argument is that these methods tend to be very low in the stack, and raising an exception that deep is really costly.

In the case of `Queue#pop`, I can already imagine some code calling `pop(timeout: 0.1)` in a loop to regularly check whether it should stop waiting, it would be a bit silly to raise an exception every time.

----------------------------------------
Feature #18774: Add Queue#pop(timeout:)
https://bugs.ruby-lang.org/issues/18774#change-97562

* 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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>