I cannot decide if the next deque is thread safe.
In short, I created a class with deque that displays its contents every 1 second in a new thread (therefore, it will not pause the main program while printing).
The deck is filled from the main thread, so basically there MUST be a chance of a collision.
HOWEVER, deque is populated using the class method, so essentially it is accessible from within the instance itself, therefore from the same stream.
Here's the simplified code:
import threading import time from collections import deque class MyQueue(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.q = deque() self.start() def run(self):
So, although adding and removing elements from the queue takes place inside the instance, is there a risk that the add function is called from outside the instance?
EDIT:
Since I need to change the elements in my deque, I had to use Deque. What I'm doing is: roatate () for a given element, pop out, change, insert it back and rotate () back to its original position.
If I donโt find a way to implement modifying elements in the queue, I will have to stick with Deque
user1102018
source share