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?
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.
Queue in Python can be implemented by the following ways: 1 list 2 collections.deque 3 queue.Queue More ...
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.
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With