SimpleQueue vs Queue in Python - what is the advantage of using SimpleQueue?
Asked Answered
V

3

30

The queue — A synchronized queue class simply states that

there are fewer functions allowed with SimpleQueue.

I need very basic queue functionality for a multithreading application, would it help in any way to use SimpleQueue?

Valenta answered 5/6, 2020 at 20:9 Comment(0)
G
19

queue.SimpleQueue handles more than threadsafe concurrency. It handles reentrancy - it is safe to call queue.SimpleQueue.put in precarious situations where it might be interrupting other work in the same thread. For example, you can safely call it from __del__ methods, weakref callbacks, or signal module signal handlers.

If you need that, use queue.SimpleQueue.

Guido answered 5/6, 2020 at 20:29 Comment(0)
P
10

The python documentations specifies that the simple queue cannot use the functionality of tracking (task_done, join). These can be used to track that every item in the queue has been processed by another process/ thread. example code:

import threading, queue

q = queue.Queue()

def worker():
    while True:
        item = q.get()
        print(f'Working on {item}')
        print(f'Finished {item}')
        q.task_done()

# turn-on the worker thread
threading.Thread(target=worker, daemon=True).start()

# send thirty task requests to the worker
for item in range(30):
    q.put(item)
print('All task requests sent\n', end='')

# block until all tasks are done
q.join()
print('All work completed')

In the above code the main thread uses join to wait for the other thread to finish processing every item it send. Meanwhile, the worker thread signals "task done" every time he handles an item in the queue. "task" is an item in the queue in this context.

Hope this helps,

for more documentation visit: https://docs.python.org/3/library/queue.html

Patin answered 5/6, 2020 at 20:50 Comment(0)
B
1

A SimpleQueue is unbounded i.e. it cannot have a maxsize and will never block on put. This can lead to unbounded memory use, and programs stalling indefinitely instead of failing with a clear error. For non-trivial programs you should prefer Queue with an appropriate maxsize.

Breskin answered 6/11, 2023 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.