On 2023-03-27 at 18:25:01 -0700,
Travis Griggs <travisgri...@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.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to