[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Thread-safe way to add a key to a dict only if it isn't already there?

Marko Rauhamaa schrieb am 07.07.2018 um 15:41:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
>> On Sat, 07 Jul 2018 02:51:41 +0900, INADA Naoki wrote:
>>> D.setdefault('c', None)
>> Oh that's clever!
> Is that guaranteed to be thread-safe? The documentation (<URL: http
> s://docs.python.org/3/library/stdtypes.html#dict.setdefault>) makes no
> such promise.

It's implemented in C and it's at least designed to avoid multiple lookups
and hash value calculations, which suggests that it's also thread-safe by
design (or by a side-effect of the design). Whether that's guaranteed, I
cannot say, but a change that makes it non-thread-safe would probably be
very controversial.

> At least __collectios_abc.py
> contains this method definition for MutableMapping:
>     def setdefault(self, key, default=None):
>         'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
>         try:
>             return self[key]
>         except KeyError:
>             self[key] = default
>         return default
> There are more such non-thread-safe definitions.

That's a different beast, because Python code can always be interrupted by
thread switches (between each byte code execution). C code cannot, unless
it starts executing byte code (e.g. for calculating a key's hash value) or
explicitly allows a thread switch at a given point.