[Python-ideas] set.add could return True or False

Jacob Holm jh at improva.dk
Fri Mar 16 12:37:24 CET 2012

On 03/14/2012 06:46 PM, Andrew Svetlov wrote:
> You still can get race condition:
> if b.add(a):
>     # some thread removes a from b before do_c call...
>     do_c()
> Explicit lock is still required for your case.

A common use for this pattern is to do some piece of work only the first 
time a given value is seen.  The race condition you mention is unlikely 
to matter there because either:

- Values are never removed from b, and so there is no race, or
- Values are only ever removed by do_c, so there is no race, or
- Whether a is actually *in* b is irrelevant to do_c.

The original race *does* matter, because do_c() may be called multiple 
times for the same value.  Changing set.add() to return True if the 
value was actually added fixes this race condition without using locks.

In fact, you could even use the proposed feature to *implement* a form 
of (non-reentrant, nonblocking) locking.  If you need a lot of locks, it 
would even be cheaper (at least memory-wise) than using threading.Lock 
objects.  Having to use a "real" lock to avoid the race condition makes 
that approach a lot less attractive.

- Jacob

> On Wed, Mar 14, 2012 at 10:36 AM, Matt Joiner<anacrolix at gmail.com>  wrote:
>> set.add(x) could return True if x was added to the set, and False if x
>> was already in the set.
>> Adding an element that is already present often constitutes an error in my code.
>> As I understand, set.add is an atomic operation. Having set.add return
>> a boolean will also allow EAFP-style code with regard to handling
>> duplicates, the long winded form of which is currently:
>> if a not in b:
>>     b.add(a)<-- race condition
>>     do_c()
>> Which can be improved to:
>> if b.add(a):
>>     do_c()
>> Advantages:
>>   * Very common code pattern.
>>   * More concise.
>>   * Allows interpreter atomicity to be exploited, often removing the
>> need for additional locking.
>>   * Faster because it avoids double contain check, and can avoid locking.
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

More information about the Python-ideas mailing list