What kind of "thread safe" are deque's actually?
Greg Ewing
greg.ewing at canterbury.ac.nz
Wed Mar 29 16:30:08 EDT 2023
On 30/03/23 6:13 am, Chris Angelico wrote:
> I'm not sure what would happen in
> a GIL-free world but most likely the lock on the input object would
> still ensure thread safety.
In a GIL-free world, I would not expect deque to hold a lock
the entire time that something was iterating over it. That
would require holding the lock as long as an iterator object
existed referencing it, which could be a long time, even
longer than the caller expects (no reference counting,
remember!)
So for future-proofing I would recommend using deque's
copy() method to copy it before doing anything that iterates
over it. Hopefully that would be implemented in a thread-safe
way (although the docs don't currently promise that).
--
Greg
More information about the Python-list
mailing list