I have a queue (from the Queue module), and I want to get indexed access into it. (i.e., being able to ask for item number four in the queue, without removing it from the queue.)
I saw that a queue uses a deque internally, and deque has indexed access. The question is, how can I use the deque without (1) messing up the queue, (2) breaking thread-safety.
It’s of course crucial to release the mutex whether the indexing succeeds or raises an IndexError, and I’m using a
withstatement for that. In older Python versions,try/finallywould be used to the same effect.