Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'exist,': 0.07; 'python': 0.09; 'dict': 0.09; 'friday,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sure,': 0.09; 'bug': 0.10; 'subject:not': 0.11; 'thread': 0.11; '2.7.3': 0.16; 'cache:': 0.16; 'naive': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:already': 0.16; 'subject:key': 0.16; 'thread-safe': 0.16; 'wrote:': 0.17; 'tracker': 0.20; 'seems': 0.23; 'url:bugs': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'older': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'chris': 0.28; "d'aprano": 0.29; 'steven': 0.29; 'url:client': 0.29; "i'm": 0.29; 'version,': 0.30; 'code': 0.31; 'url:python': 0.32; 'received:com.au': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'subject:?': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'received:au': 0.36; 'possible': 0.37; 'subject:: ': 0.38; 'url:en': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'between': 0.63; 'manner.': 0.66; 'wish': 0.70; 'race': 0.71; 'url:search': 0.83; 'investigated': 0.84; 'url:cd': 0.84; 'url:clnk': 0.84; 'url:googleusercontent': 0.84; 'subject:add': 0.91; 'url:cache': 0.93; 'url:ct': 0.93; 'received:110': 0.96 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Lie Ryan Subject: Re: Safely add a key to a dict only if it does not already exist? Date: Sat, 19 Jan 2013 16:00:25 +1100 References: <50fa1bf1$0$30003$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 110-175-240-90.static.tpgi.com.au User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358571639 news.xs4all.nl 6961 [2001:888:2000:d::a6]:53936 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37049 On 19/01/13 15:15, Chris Rebert wrote: > On Friday, January 18, 2013, Steven D'Aprano wrote: > > I wish to add a key to a dict only if it doesn't already exist, but > do it > in a thread-safe manner. > > The naive code is: > > if key not in dict: > dict[key] = value > > > but of course there is a race condition there: it is possible that > > another thread may have added the same key between the check and the > store. > > How can I add a key in a thread-safe manner? > > > I'm not entirely sure, but have you investigated dict.setdefault() ? dict.setdefault() was not atomic on older python version, they were made atomic in Python 2.7.3 and Python 3.2.3. See bug13521 in the issue tracker http://bugs.python.org/issue13521 PS: The bug tracker seems down at the moment, so pulled this from Google's cache: https://webcache.googleusercontent.com/search?q=cache:59PO_F-VEfwJ:bugs.python.org/issue13521+&cd=1&hl=en&ct=clnk&client=ubuntu