Is this deque thread-safe in python? Is this deque thread-safe in python? python python

Is this deque thread-safe in python?


Deque is thread-safe (http://docs.python.org/library/collections.html#deque-objects) for appends and pops from opposite sides. Beneath here, the docs only mention that append() and popleft() are thread-safe.

There is a thread-safe implementation of the Queue itself. So you should be using it unless you have some strange requirements.


Deque author here.

The MyQueue() class looks correct to me (at least with respect to the question of thread-safety).

The append() and popleft() methods are both atomic.

The code does need EAFP logic to handle the case where the input is empty:

def run(self):    while True:        try:            print self.q.popleft()        except IndexError:            pass        time.sleep(1)


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 is:

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 for print self.q.popleft() and self.q.append(val) ;)