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

Marko Rauhamaa marko at pacujo.net
Sat Jul 7 17:00:26 EDT 2018


Ian Kelly <ian.g.kelly at gmail.com>:
> the leaning of the devs seems to be to refrain from documenting it and
> instead document that *no* operations are guaranteed atomic.

I believe that to be wise. Otherwise, Python would limit its future
implementation options.

The only thing Python should guarantee is that the data structures stay
"coherent" under race conditions. In other words, there cannot be a
segmentation fault. For example, if two threads executed this code in
parallel:

    global i
    i = 1
    i += 1

a legal end result could be that i contains the string "impossible".


Marko


More information about the Python-list mailing list