Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37085

Re: Safely add a key to a dict only if it does not already exist?

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Safely add a key to a dict only if it does not already exist?
Date 2013-01-19 08:04 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-EE3E08.08041519012013@news.panix.com> (permalink)
References <50fa1bf1$0$30003$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


In article <50fa1bf1$0$30003$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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?

You want something along the lines of:

from threading import Lock
lock = Lock()
[...]
lock.acquire()
if key not in dict:
   dict[key] = value
lock.release()

You probably want to wrap that up in a context manager to ensure the 
lock is released if you get an exception.  You don't want your entire 
program to hang just because somebody handed you a key which wasn't 
hashable, for example.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Safely add a key to a dict only if it does not already exist? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-19 04:07 +0000
  Re: Safely add a key to a dict only if it does not already exist? Chris Rebert <clp2@rebertia.com> - 2013-01-18 20:15 -0800
    Re: Safely add a key to a dict only if it does not already exist? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-19 10:31 +0000
  Re: Safely add a key to a dict only if it does not already exist? Lie Ryan <lie.1296@gmail.com> - 2013-01-19 16:00 +1100
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 08:25 +0100
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 08:27 +0100
  Re: Safely add a key to a dict only if it does not already exist? Mitya Sirenef <msirenef@lightbird.net> - 2013-01-19 02:35 -0500
  Re: Safely add a key to a dict only if it does not already exist? Peter Otten <__peter__@web.de> - 2013-01-19 09:02 +0100
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 09:16 +0100
  Re: Safely add a key to a dict only if it does not already exist? Mitya Sirenef <msirenef@lightbird.net> - 2013-01-19 03:18 -0500
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 09:19 +0100
  Re: Safely add a key to a dict only if it does not already exist? Chris Angelico <rosuav@gmail.com> - 2013-01-19 19:23 +1100
  Re: Safely add a key to a dict only if it does not already exist? Roy Smith <roy@panix.com> - 2013-01-19 08:04 -0500

csiph-web