Queue.Queue vs. collections.deque

前端 未结 7 1391
眼角桃花
眼角桃花 2020-11-29 15:26

I need a queue which multiple threads can put stuff into, and multiple threads may read from.

Python has at least two queue classes, Queue.Queue and collections.dequ

7条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 15:36

    deque is thread-safe. "operations that do not require locking" means that you don't have to do the locking yourself, the deque takes care of it.

    Taking a look at the Queue source, the internal deque is called self.queue and uses a mutex for accessors and mutations, so Queue().queue is not thread-safe to use.

    If you're looking for an "in" operator, then a deque or queue is possibly not the most appropriate data structure for your problem.

提交回复
热议问题