Ram*_*hum 8 python queue multithreading deque
我有一个队列(来自Queue模块),我希望获得索引访问权限.(即,能够在队列中询问第4项,而不将其从队列中删除.)
我看到一个队列在内部使用了一个deque,而deque已经建立了索引访问.问题是,如何在没有(1)弄乱队列的情况下使用双端队列,(2)破坏线程安全性.
Ale*_*lli 11
import Queue
class IndexableQueue(Queue):
def __getitem__(self, index):
with self.mutex:
return self.queue[index]
Run Code Online (Sandbox Code Playgroud)
无论索引成功还是引发IndexError,释放互斥锁当然是至关重要的,我正在使用一个with语句.在较旧的Python版本中,try/ finally将用于相同的效果.