Path: csiph.com!usenet.pasdenom.info!news.albasani.net!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'exist,': 0.07; 'dict': 0.09; 'friday,': 0.09; 'sure,': 0.09; 'cc:addr:python-list': 0.10; 'subject:not': 0.11; 'thread': 0.11; 'naive': 0.16; 'subject:already': 0.16; 'subject:key': 0.16; 'thread-safe': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.17; 'cheers,': 0.23; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'chris': 0.28; "d'aprano": 0.29; 'steven': 0.29; "i'm": 0.29; 'code': 0.31; 'another': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'add': 0.36; 'but': 0.36; 'possible': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'between': 0.63; 'manner.': 0.66; 'wish': 0.70; 'race': 0.71; 'investigated': 0.84; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84; 'subject:add': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=f6M72dYjAkdSL1KAqbS52uG802/Grqu6tyMwhNgAVk4=; b=VUkIcJic62f25467mPbzRPgtR/1c9A/4ct7RCR0TDK405HRF6rny4uaz0T/V4ZwUN1 weS/Lm2b1eN1KIwgCg3wZkEsu1C0jBZJFVksJSKz6GwaYiRtHXxlvD6FmLjhr+X8w9Bm WKsrka3pz79BHtgmoUvPcOFDcCahxC4vyqhyU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=f6M72dYjAkdSL1KAqbS52uG802/Grqu6tyMwhNgAVk4=; b=oYFWS1adNW45+jplDrC110Z8UGDfx/KLye5ucZwhVF5SEnEVDdar6sb0vCmTmp61jm dZnYT+dGRni4ny+SGgMiuQoO9bH3+e9sMm06r5WBb4xLxYwHP2s8WWFQ0FaA6VyB458+ V9yXOVu5/rTJSeAMLh+1kMMkuvPKO6SqMjFUPZgVDAIH/BelBkXjEK0NacMN9UoREk3C dN0fitXND3fAJbAciKbvWQKR+0NrVBeyg8W5JRLTVpr3y1pOqjVrdQ+Oi4SKytp54Esh cusRUHmn4BsbetyqbF9uMlxfOlQ8iE3U3VOgjJG3gyDLQRK8QeE3BS/bTTG1NDJTDB2o N4OA== MIME-Version: 1.0 X-Received: by 10.50.150.239 with SMTP id ul15mr3832875igb.93.1358568926338; Fri, 18 Jan 2013 20:15:26 -0800 (PST) Sender: chris@rebertia.com In-Reply-To: <50fa1bf1$0$30003$c3e8da3$5496439d@news.astraweb.com> References: <50fa1bf1$0$30003$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 18 Jan 2013 20:15:26 -0800 X-Google-Sender-Auth: -gv-S4fUG0ZG4Lkqf4wFA5mACfs Subject: Re: Safely add a key to a dict only if it does not already exist? From: Chris Rebert To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=f46d042fdbca41dca504d39c7a3e X-Gm-Message-State: ALoCoQl3Ai6Chkd+A+7nFZTV/x2c/AciHTbQBrpKfGp/E6FHCxMISwNMBT/h7KnMuCQMSh74EB3t Cc: "python-list@python.org" 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: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358568929 news.xs4all.nl 6910 [2001:888:2000:d::a6]:41318 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37046 --f46d042fdbca41dca504d39c7a3e Content-Type: text/plain; charset=UTF-8 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() ? -- Cheers, Chris -- http://rebertia.com --f46d042fdbca41dca504d39c7a3e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Friday, January 18, 2013, Steven D'Aprano wrote:
I wish to add a key to a dict only if it doesn't alread= y exist, but do it
in a thread-safe manner.

The naive code is:

if key not in dict:
=C2=A0 =C2=A0 dict[key] =3D 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 in= vestigated dict.setdefault() ?


--
Cheers,
= Chris
--
http://reb= ertia.com
--f46d042fdbca41dca504d39c7a3e--