[Python] Having the deque with the advantages of Queue in a thread

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I need a structure where I will be able to pop() and append() to the right side (just like deque), while having the structure blocking and waiting if it is empty (just like Queue). I could directly use a Queue, but I need also the nice feature of deque where items are removed without blocking if the structure is full.

    from collections import deque

    d = deque(maxlen=2)
    d.append(1)
    d.append(2)
    d.append(3) # d should be [2,3] (it is the case)
    d.pop()
    d.pop()
    d.pop() # should wait (not the case)


    Is it better to subclass deque (making it wait) or Queue (adding a popLeft function)?

    Continue reading...

Compartilhe esta Página