I have a dictionary updated by one thread, and in another thread I would like to iterate over its values. I usually use locking, but this code is very performance critical, and I want to avoid this, if at all possible.
A feature of my case is that I do not care about the absolute correctness of the iterator; if it skips records that were deleted after the iteration started, or after that adds those that were added later, thatβs fine. I only require that it does not cause any "dictionary size changed during iteration".
Given this relaxed restriction on correctness, is there an effective way to iterate a dictionary without using locks?
Note. I know it keys()is thread safe in Python 2.x, but since this behavior has changed in 3.x, I want to avoid it.
source
share