Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 362
Josh Bone Avatar asked Jun 05 '20 20:06

Josh Bone


People also ask

How to implement enqueue in Python?

Queue in Python can be implemented by the following ways: List is a Python’s built-in data structure that can be used as a queue. Instead of enqueue () and dequeue (), append () and pop () function is used.

How to implement queue in Python?

Queue in Python can be implemented by the following ways: 1 list 2 collections.deque 3 queue.Queue More ...

What is the difference between queue queue and collections deque?

We also know, that two threads may have to communicate with each other and this is where queue.queue comes into the picture. Collections.deque on the other hand is used as a data structure within a thread to perform certain functionality. The link between them is that queue.queue uses collections.deque internally.

How does simple queue handle reentrancy?

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.


2 Answers

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.

like image 178
user2357112 supports Monica Avatar answered Oct 24 '22 03:10

user2357112 supports Monica


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

like image 6
Amit Berger Avatar answered Oct 24 '22 03:10

Amit Berger