[Python-Dev] Retrieve an arbitrary element from a setwithoutremoving it

Steven D'Aprano steve at pearwood.info
Fri Nov 6 07:01:01 CET 2009

On Fri, 6 Nov 2009 10:52:54 am Yuvgoog Greenle wrote:
> On Fri, Nov 6, 2009 at 1:17 AM, James Y Knight <foom at fuhm.net> wrote:
> > Is this thread over yet?
> Sorry, I just had to point out that pop/add has a side effect that
> would be apparent on a set that multiple threads access - it loses an
> item and then gets it back. Sounds like a sleeper race condition
> that's going to be rare but extremely hard to find if it does occur.
> Crooked as a gil.

Surely Raymond's suggestion also suffers from a similar race condition?

for x in set:
    return x

creates a set_iterator. If another thread modifies the original set 
after the set_iterator is created but before the return, you would get 
a mysterious and almost impossible to debug RuntimeError.

Steven D'Aprano

More information about the Python-Dev mailing list