deque and thread-safety

Antoine Pitrou solipsis at pitrou.net
Fri Oct 12 09:36:24 EDT 2012


Hello,

Christophe Vandeplas <christophe <at> vandeplas.com> writes:
> 
> From the documentation I understand that deques are thread-safe:
> > Deques are a generalization of stacks and queues (the name is pronounced 
“deck”
> > and is short for “double-ended queue”). Deques support thread-safe, memory
> > efficient appends and pops from either side of the deque with approximately 
the
> > same O(1) performance in either direction.
> 
> It seems that appending to deques is indeed thread-safe, but not
> iterating over them.

Indeed, if you read the above sentence, deques only support "thread-safe [...] 
appends and pops". Other operations are not necessarily thread-safe.
Moreover, any operation which will call back into Python (such as iterating
several times) are *not* thread-safe.

Regardless, deques are not the right container for containment tests
("url in seen"). Like with lists or tuples, a containment test in a deque will 
be O(n). So if you want efficient containment tests, you should use a set or a 
dict.

Regards

Antoine.





More information about the Python-list mailing list