What kind of "thread safe" are deque's actually?
2QdxY4RzWzUUiLuE at potatochowder.com
2QdxY4RzWzUUiLuE at potatochowder.com
Mon Mar 27 21:41:13 EDT 2023
On 2023-03-27 at 18:25:01 -0700,
Travis Griggs <travisgriggs at gmail.com> wrote:
> "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.”
> (https://docs.python.org/3.11/library/collections.html?highlight=deque#collections.deque)
[...]
> I guess this surprised me. When I see “thread safe”, I don’t expect to
> get errors.
Even without threads, mutating a collection while iterating over it
usually results in bad things happening.
$ python
Python 3.10.10 (main, Mar 5 2023, 22:26:53) [GCC 12.2.1 20230201] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> x = collections.deque()
>>> x.append(44)
>>> x.append(55)
>>> x.append(66)
>>> x.append(77)
>>> x
deque([44, 55, 66, 77])
>>> for y in x:
x.pop()
77
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: deque mutated during iteration
Concurrency just increases the likeliness of mutating while iterating.
More information about the Python-list
mailing list