Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sure.': 0.05; 'brad': 0.07; 'skip:` 10': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; 'def': 0.13; 'subject:function': 0.16; 'wrote:': 0.18; 'seems': 0.20; 'header :In-Reply-To:1': 0.22; 'bit': 0.28; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'to:addr:python-list': 0.34; 'received:au': 0.36; 'run': 0.37; 'received:org': 0.38; 'should': 0.39; 'to:addr:python.org': 0.40; '01:30': 0.84; 'received:110': 0.95; 'subject:Make': 0.96 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Lie Ryan Subject: Re: Make a small function thread safe Date: Sat, 17 Dec 2011 09:08:04 +1100 References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> 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:8.0) Gecko/20111124 Thunderbird/8.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324073306 news.xs4all.nl 6860 [2001:888:2000:d::a6]:40607 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17389 On 12/17/2011 01:30 AM, Brad Tilley wrote: > Or perhaps run should look like this instead: > > def run(t): > lock.acquire() > shared_container.append(t.name ) > lock.release() > > That seems a bit barbaric to me, not sure. change that to: def run(t): with lock: shared_container.append(t.name ) the `with-statement` will call lock.acquire() and lock.release().