I suggest replacing the use of Queue.Queue with collections.deque . The Queue class is designed for synchronous communication between threads, so when used as a regular data structure, it will have unnecessary overhead. collections.deque is a faster alternative. (The name βdequeβ is pronounced βdeckβ and means βdouble line.β)
The deque class has a different API than the Queue type, but it is fairly easy to translate between them. Use deque.append instead of Queue.put and deque.popleft instead of q.get() (or appendleft and pop if you like to go in the other direction). Instead of calling Queue.empty just use the deque instance as a boolean (for example, to check for an empty list).
deque instances are picklable:
>>> import collections, pickle >>> q = collections.deque(["test"]) >>> pickle.dumps(q) b'\x80\x03ccollections\ndeque\nq\x00]q\x01X\x04\x00\x00\x00testq\x02a\x85q\x03Rq\x04.'
source share