ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek`
@ 2023-09-28 23:10 hi@joaofernandes.me (Joao Fernandes) via ruby-core
  2023-09-29  0:43 ` [ruby-core:114916] " ko1 (Koichi Sasada) via ruby-core
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: hi@joaofernandes.me (Joao Fernandes) via ruby-core @ 2023-09-28 23:10 UTC (permalink / raw
  To: ruby-core; +Cc: hi@joaofernandes.me (Joao Fernandes)

Issue #19905 has been reported by hi@joaofernandes.me (Joao Fernandes).

----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:114916] [Ruby master Feature#19905] Introduce `Queue#peek`
  2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
@ 2023-09-29  0:43 ` ko1 (Koichi Sasada) via ruby-core
  2023-09-29 11:16 ` [ruby-core:114921] " hi@joaofernandes.me (Joao Fernandes) via ruby-core
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: ko1 (Koichi Sasada) via ruby-core @ 2023-09-29  0:43 UTC (permalink / raw
  To: ruby-core; +Cc: ko1 (Koichi Sasada)

Issue #19905 has been updated by ko1 (Koichi Sasada).


Could you describe more about use cases?

----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905#change-104784

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:114921] [Ruby master Feature#19905] Introduce `Queue#peek`
  2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
  2023-09-29  0:43 ` [ruby-core:114916] " ko1 (Koichi Sasada) via ruby-core
@ 2023-09-29 11:16 ` hi@joaofernandes.me (Joao Fernandes) via ruby-core
  2023-10-06 17:28 ` [ruby-core:114963] " shan (Shannon Skipper) via ruby-core
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: hi@joaofernandes.me (Joao Fernandes) via ruby-core @ 2023-09-29 11:16 UTC (permalink / raw
  To: ruby-core; +Cc: hi@joaofernandes.me (Joao Fernandes)

Issue #19905 has been updated by hi@joaofernandes.me (Joao Fernandes).


ko1 (Koichi Sasada) wrote in #note-1:
> Could you describe more about use cases?

My main use case for this feature is observability and debugging. For example, I am experimenting with a car pooling simulation where multiple queues of passengers exist. I want to track the length of each queue, and how long the first passenger of each queue has been waiting. Thus, I would like to look at the first element of each queue without removing it, so that the workers can pick it up.

I was also considering using this method to decide which queue to serve first. While I would still need to be careful with concurrency issues for this feature, `#peek` would be extremely helpful.

Does this clarify potential use cases? Does it seem like a potential good addition to the language?


----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905#change-104789

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:114963] [Ruby master Feature#19905] Introduce `Queue#peek`
  2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
  2023-09-29  0:43 ` [ruby-core:114916] " ko1 (Koichi Sasada) via ruby-core
  2023-09-29 11:16 ` [ruby-core:114921] " hi@joaofernandes.me (Joao Fernandes) via ruby-core
@ 2023-10-06 17:28 ` shan (Shannon Skipper) via ruby-core
  2023-10-06 22:20 ` [ruby-core:114964] " Eregon (Benoit Daloze) via ruby-core
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: shan (Shannon Skipper) via ruby-core @ 2023-10-06 17:28 UTC (permalink / raw
  To: ruby-core; +Cc: shan (Shannon Skipper)

Issue #19905 has been updated by shan (Shannon Skipper).


I'm just curious if it might be better for #peek to lock to avoid showing something that has already popped or other parallel access issues? Is there significant advantage having it not lock?

----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905#change-104834

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:114964] [Ruby master Feature#19905] Introduce `Queue#peek`
  2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
                   ` (2 preceding siblings ...)
  2023-10-06 17:28 ` [ruby-core:114963] " shan (Shannon Skipper) via ruby-core
@ 2023-10-06 22:20 ` Eregon (Benoit Daloze) via ruby-core
  2024-02-13 14:59 ` [ruby-core:116699] " sidonath (Damir Zekic) via ruby-core
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2023-10-06 22:20 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #19905 has been updated by Eregon (Benoit Daloze).


`peek` is inherently racy in that the value observed might be already popped.
If it's for observability and debugging I think `to_a` or `each` makes more sense, it's for sure useful to see other values than just the first one.

----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905#change-104835

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:116699] [Ruby master Feature#19905] Introduce `Queue#peek`
  2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
                   ` (3 preceding siblings ...)
  2023-10-06 22:20 ` [ruby-core:114964] " Eregon (Benoit Daloze) via ruby-core
@ 2024-02-13 14:59 ` sidonath (Damir Zekic) via ruby-core
  2024-02-13 17:22 ` [ruby-core:116704] " byroot (Jean Boussier) via ruby-core
  2024-02-13 19:05 ` [ruby-core:116714] " ko1 (Koichi Sasada) via ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: sidonath (Damir Zekic) via ruby-core @ 2024-02-13 14:59 UTC (permalink / raw
  To: ruby-core; +Cc: sidonath (Damir Zekic)

Issue #19905 has been updated by sidonath (Damir Zekic).


I would like to provide another use case for `Queue#peek`. I'm using `Queue` in a project that's running commands prone to failures. I have a queue of commands, a worker and a monitor threads. The worker pops the commands, runs them in sequence and the monitor thread reports on the activity and restarts the worker if it observes a failure. Having ability to "peek" into the command list would allow informative output about what is the next command that will be executed. Obviously, having `#to_a` or `#each` would be as helpful here. The queue is not long for me to worry about looking up all elements versus the first one.

If there's a race condition and the proposed `#peek` doesn't lock, then the display of the "next command" might be briefly inaccurate at times, but that's not something I would consider an issue in this project. If it were locking, then I would need to use the retrieved instance outside of the locked block. This would beat the purpose of the lock, but that would be the conscious choice of me as the author of the code. I guess whether `#peek` locks or not would not be important in my situation.

----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905#change-106714

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:116704] [Ruby master Feature#19905] Introduce `Queue#peek`
  2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
                   ` (4 preceding siblings ...)
  2024-02-13 14:59 ` [ruby-core:116699] " sidonath (Damir Zekic) via ruby-core
@ 2024-02-13 17:22 ` byroot (Jean Boussier) via ruby-core
  2024-02-13 19:05 ` [ruby-core:116714] " ko1 (Koichi Sasada) via ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-02-13 17:22 UTC (permalink / raw
  To: ruby-core; +Cc: byroot (Jean Boussier)

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


Seems like all the use cases so far would be as well if not better handled by `to_a`. Which I admit I hoped existed a few times when debugging code that uses `Queue`. Should also be relatively straightforward to implement.

----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905#change-106720

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:116714] [Ruby master Feature#19905] Introduce `Queue#peek`
  2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
                   ` (5 preceding siblings ...)
  2024-02-13 17:22 ` [ruby-core:116704] " byroot (Jean Boussier) via ruby-core
@ 2024-02-13 19:05 ` ko1 (Koichi Sasada) via ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: ko1 (Koichi Sasada) via ruby-core @ 2024-02-13 19:05 UTC (permalink / raw
  To: ruby-core; +Cc: ko1 (Koichi Sasada)

Issue #19905 has been updated by ko1 (Koichi Sasada).


I feel it is natural that `to_a` returns `[]` immediately if there is no enqueued item.
(`#peek` is not clear on it)

----------------------------------------
Feature #19905: Introduce `Queue#peek`
https://bugs.ruby-lang.org/issues/19905#change-106730

* Author: hi@joaofernandes.me (Joao Fernandes)
* Status: Open
* Priority: Normal
----------------------------------------
This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock.

```
q = Queue.new([1,2,3])
=> #<Thread::Queue:0x00000001065d7148>
q.peek
=> 1
q.peek
=> 1
```

I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe.

I also volunteer myself to implement this method.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-02-13 19:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-28 23:10 [ruby-core:114915] [Ruby master Feature#19905] Introduce `Queue#peek` hi@joaofernandes.me (Joao Fernandes) via ruby-core
2023-09-29  0:43 ` [ruby-core:114916] " ko1 (Koichi Sasada) via ruby-core
2023-09-29 11:16 ` [ruby-core:114921] " hi@joaofernandes.me (Joao Fernandes) via ruby-core
2023-10-06 17:28 ` [ruby-core:114963] " shan (Shannon Skipper) via ruby-core
2023-10-06 22:20 ` [ruby-core:114964] " Eregon (Benoit Daloze) via ruby-core
2024-02-13 14:59 ` [ruby-core:116699] " sidonath (Damir Zekic) via ruby-core
2024-02-13 17:22 ` [ruby-core:116704] " byroot (Jean Boussier) via ruby-core
2024-02-13 19:05 ` [ruby-core:116714] " ko1 (Koichi Sasada) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).