Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'binary': 0.05; 'elegant': 0.07; 'subject:file': 0.07; 'subject:two': 0.07; 'python': 0.09; 'objects.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:same': 0.09; 'thread': 0.11; 'file,': 0.15; 'passing': 0.15; 'deadlock': 0.16; 'devise': 0.16; 'from:addr:pitrou.net': 0.16; 'from:addr:solipsis': 0.16; 'from:name:antoine pitrou': 0.16; 'message-id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'record,': 0.16; 'subject:Writing': 0.16; 'subject:threads': 0.16; 'thread-safe': 0.16; 'unsafe': 0.16; 'wrote:': 0.17; '(not': 0.20; 'preferred': 0.20; 'written': 0.20; 'paul': 0.24; 'header :User-Agent:1': 0.26; '(e.g.': 0.27; 'header:X-Complaints-To:1': 0.28; 'diagnose': 0.29; 'i/o': 0.29; 'issues.': 0.29; 'locking': 0.29; 'locks': 0.29; 'question:': 0.29; 'queue': 0.29; 'writes:': 0.29; 'that.': 0.30; 'point': 0.31; 'gets': 0.32; 'not.': 0.32; 'doubt': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'another': 0.33; 'text': 0.34; 'wrong': 0.34; 'done': 0.34; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; '(i.e.': 0.36; 'thank': 0.36; 'charset:us-ascii': 0.36; 'why': 0.37; 'communicate': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'you.': 0.61; 'situation': 0.62; 'between': 0.63; 'safe': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'antoine.': 0.84; 'received:88.163': 0.84; 'suspicion': 0.84; 'safer': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Antoine Pitrou Subject: Re: Writing to same file from two threads Date: Wed, 27 Feb 2013 13:26:18 +0000 (UTC) References: <7xliabvv4g.fsf@ruckus.brouhaha.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 88.163.232.20 (Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0) 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361971792 news.xs4all.nl 6897 [2001:888:2000:d::a6]:50699 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40061 Jens Thoms Toerring toerring.de> writes: > > Paul Rubin nospam.invalid> wrote: > > jt toerring.de (Jens Thoms Toerring) writes: > > > in garbled output (i.e. having some output from A inside a > > > line written by B or vice versae) because the "main thread" or > > > Yes they do get garbled like that. Preferred Python style is put a > > single thread in charge of all the i/o to that file, and communicate > > with it by message passing through Queue objects. That is safer than > > directly using locks. > > Thank you for confirmig my suspicion But you have induced > another question: why is using a Queue safer than locking (not > that I doubt that it might be more elegant etc.). Is it "safer" > because it's less likely that one gets it wrong (e.g. by for- > grtting to acquire the lock) or is there something inherently > unsafe about locks? For the record, binary files are thread-safe in Python 3, but text files are not. Locks are safe if you use them well. As you point out, if you forget to acquire your lock, or if you devise a situation where there is a deadlock between competing locks, you can have difficult to diagnose issues. Queues have their internal locking all done for you. Regards Antoine.