0

I would like to have a persistant queue in python that I can cycle through, but only remove items if a particular condition is met.

I have seen the persist-queue package, but queue.get() removes the item without allowing checks first. Does anyone know of a package/approach to do this (other than using persist-queue and adding items back onto the queue if the condition isn't met).

So for example:

I have a queue containing the following items:

[1,2,37,8,9,3,10,22]

while not queue.empty:
    item = queue.get()
    if item % 3 ==0:
        remove item from queue

should leave the queue with the following items:

[1,2,37,8,10,22]

abinitio
  • 609
  • 6
  • 20
  • what do you mean "it seems to pop each item off the queue"? It will pop items if you request them to be popped. Just don't pop them if you don't want them out. –  Jul 07 '22 at 10:43
  • I don't know what the items in the queue will be - i need to cycle through all members of the queue, removing only the items that cause a condition to be met. persist-qeueue uses queue.get() to get the next item in the queue, and q.task_done() to write. But there is no way to mark an item to stay. – abinitio Jul 07 '22 at 11:16
  • maybe you need a list instead of a queue then. –  Jul 07 '22 at 11:18
  • But I want it to be persistent - in case there is a crash I need to have a record of the queue. I can write the queue to file, but was hoping there is a package that already has this behaviour for persistent queues. – abinitio Jul 07 '22 at 11:21
  • https://stackoverflow.com/questions/9449674/how-to-implement-a-persistent-python-list or https://github.com/tobgu/pyrsistent –  Jul 07 '22 at 11:22

0 Answers0