Python Forum
Is there an iterable queue in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there an iterable queue in python?
#1
Python 3.7.1

I found queue class in the standard library. Unfortunately, in the help it's not written that it's iterable, but I need queue that provide iteration in back direction (without deleting elements from queue, to read items only).

How I can decide this question?
Reply
#2
You could perhaps use a collections.deque instance
>>> from collections import deque
>>> d = deque(range(10))
>>> for item in reversed(d):
...     print(item)
... 
9
8
7
6
5
4
3
2
1
0
Reply
#3
The class Queue is not subscriptable nor iterable.
A naive way to add iteration support to the Queue class, is done by inheritance and magic methods:

class IterQueue(queue.Queue):
    def __iter__(self):
        while True:
            yield self.get()
If you iterate over this object, the iteration never stops. Creating finite lists from this object was not my intention.

The deque object is also thread safe, so you can use it in threads, but it's a different implementation as a queue.

A queue blocks (default behavior) for example, if the queue is full (given maxsize).
A deque object does not block, if the maxsize has been reached. In this case the last or first element is dropped from the list.

In addition you can join a queue object. For each submitted element to the queue, you've to call queue.task_done().
Joining the queue blocks until all taks are marked as done. The deque object don't have this mechanism.

A deque object is not a drop-in-replacement for queue objects.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Gribouillis, thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  queue for async function python telegram.ext noctious 0 1,512 Jun-11-2023, 02:58 PM
Last Post: noctious
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,524 May-24-2022, 12:03 PM
Last Post: deanhystad
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,096 Aug-10-2021, 08:10 AM
Last Post: snippsat
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 8,819 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 5,865 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  task queue Valon1981 8 3,517 Jul-07-2020, 07:41 AM
Last Post: freeman
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,016 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  TypeError: 're.Match' object is not iterable charlesauspicks 1 11,491 May-25-2020, 06:14 AM
Last Post: bowlofred
  function/nonetype object is not iterable nanok66 5 3,966 May-08-2020, 07:39 PM
Last Post: nanok66
  min() function in iterable index OokaydO 4 2,910 Apr-23-2020, 09:21 AM
Last Post: OokaydO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020