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


Groups > comp.lang.python > #33980

Re: changing dicts in a threaded environment ?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'essentially': 0.04; 'explicitly': 0.04; 'interpreter': 0.04; 'python3': 0.05; 'builtin': 0.07; 'wrapper': 0.07; 'dict': 0.09; 'lookup': 0.09; 'occasionally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'terry': 0.09; 'threads.': 0.09; 'underlying': 0.09; 'thread': 0.11; 'properly': 0.15; '"real"': 0.16; 'already,': 0.16; 'crashed': 0.16; 'crashes': 0.16; 'iterator': 0.16; 'lookups': 0.16; 'naive': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'runtimeerror': 0.16; 'subject:changing': 0.16; 'thead': 0.16; 'truely': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'jan': 0.18; 'appropriate': 0.20; 'parameters': 0.20; 'modifying': 0.22; 'elements': 0.23; 'user.': 0.23; 'second': 0.24; 'url:bugs': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'prevent': 0.27; 'wonder': 0.27; 'question': 0.27; 'header:X-Complaints-To:1': 0.28; 'actual': 0.28; 'locking': 0.29; 'queue': 0.29; 'wrap': 0.29; 'objects': 0.29; 'probably': 0.29; 'usually': 0.30; 'worked': 0.30; 'subject: ?': 0.30; 'stuff': 0.30; 'gets': 0.32; 'url:python': 0.32; 'not.': 0.32; 'instead,': 0.33; 'raising': 0.33; 'to:addr:python-list': 0.33; 'changed': 0.34; 'done': 0.34; 'whatever': 0.35; 'doing': 0.35; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'possible': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'remove': 0.61; 'first': 0.61; 'safe': 0.63; 'skip:n 10': 0.63; 'manner.': 0.66; '<>).': 0.84; 'arrived.': 0.84; 'dict,': 0.84; 'flag.': 0.84; 'iterative': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: changing dicts in a threaded environment ?
Date Tue, 27 Nov 2012 14:18:15 -0500
References <CADPXuAh770FeNFPB9Bw-TqYE=YuZxMuv5BTOebTgCZRz2tAxyw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2
In-Reply-To <CADPXuAh770FeNFPB9Bw-TqYE=YuZxMuv5BTOebTgCZRz2tAxyw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.321.1354043931.29569.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354043931 news.xs4all.nl 6948 [2001:888:2000:d::a6]:49719
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33980

Show key headers only | View raw


On 11/27/2012 7:53 AM, Bart Thate wrote:

[Answers based on reading without thread experience.]

> i use python3 now and i need to be able to remove elements from a dict
> in a thread safe manner.

Essentially no change from py2.

> kinda like a Queue.Queue thing but then in a dict, so i can pass arond
>   my dict based objects as parameters arond without having to wonder if
> it gets properly locked.

As I understand it, dicts do not get locked unless you do it.

> So not only do i need to get this solved:
>
> Issue #14417 <http://bugs.python.org/14417>: Mutating a dict during
> lookup now restarts the lookup instead of raising a RuntimeError (undoes
> issue #14205 <http://bugs.python.org/14205>).

As I understand #14417, you should be explicitly locking dicts. The 
issue in #14205 was that people doing mutations in just one thread and 
lookups in others usually got away without locking becuase of recursive 
retries, but occasionally crashed the interpreter because of them. The 
first fix was to prevent crashes by removing retries. But that would 
break programs that naively worked because of them. The second fix was 
to do iterative retries instead, so a thread might occasionally hang, 
but without crashing.

As I understand it, it is better to not be a naive user.

> i also need to lock all the actual modifying underlying "real" stuff as
> well not just the iterator or view or whatever i don't know yet ;]

I am not sure what you are asking.
>
> So my question is kinda like, is a dict possible in the same way the
> Queue.Queue is now made truely thread safe ?

A custom dict might be but the builtin is not. Queue is somewhat unique 
as a builtin designed for threads.

> Also is there something like a select.select for queues ?

Have you searched?

> I want to pass a dict to a thead and then have a watcher on the dicts
> state if result have arrived.

Builtin dicts do not have an 'I have changed flag. You would need to 
subclass or probably better, wrap a dict using the appropriate pattern. 
Perhaps done already, but you want a wrapper that does both locking and 
watcher notification.

-- 
Terry Jan Reedy

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


Thread

Re: changing dicts in a threaded environment ? Terry Reedy <tjreedy@udel.edu> - 2012-11-27 14:18 -0500

csiph-web