Queue.Queue vs. collections.deque

前端 未结 7 1388
眼角桃花
眼角桃花 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:45

    For information there is a Python ticket referenced for deque thread-safety (https://bugs.python.org/issue15329). Title "clarify which deque methods are thread-safe"

    Bottom line here: https://bugs.python.org/issue15329#msg199368

    The deque's append(), appendleft(), pop(), popleft(), and len(d) operations are thread-safe in CPython. The append methods have a DECREF at the end (for cases where maxlen has been set), but this happens after all of the structure updates have been made and the invariants have been restored, so it is okay to treat these operations as atomic.

    Anyway, if you are not 100% sure and you prefer reliability over performance, just put a like Lock ;)

提交回复
热议问题