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


Groups > comp.lang.python > #40061

Re: Writing to same file from two threads

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 <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; '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 <solipsis@pitrou.net>
Subject Re: Writing to same file from two threads
Date Wed, 27 Feb 2013 13:26:18 +0000 (UTC)
References <ap420lFiectU1@mid.uni-berlin.de> <7xliabvv4g.fsf@ruckus.brouhaha.com> <ap4j15Fmb9lU1@mid.uni-berlin.de>
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 <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.2618.1361971792.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


Jens Thoms Toerring <jt <at> toerring.de> writes:
> 
> Paul Rubin <no.email <at> nospam.invalid> wrote:
> > jt <at> 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.

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


Thread

Writing to same file from two threads jt@toerring.de (Jens Thoms Toerring) - 2013-02-26 15:17 +0000
  Re: Writing to same file from two threads Paul Rubin <no.email@nospam.invalid> - 2013-02-26 09:32 -0800
    Re: Writing to same file from two threads jt@toerring.de (Jens Thoms Toerring) - 2013-02-26 20:08 +0000
      Re: Writing to same file from two threads Antoine Pitrou <solipsis@pitrou.net> - 2013-02-27 13:26 +0000
        Re: Writing to same file from two threads jt@toerring.de (Jens Thoms Toerring) - 2013-02-27 16:05 +0000
        Re: Writing to same file from two threads Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-28 01:05 +0000
          Re: Writing to same file from two threads Antoine Pitrou <solipsis@pitrou.net> - 2013-03-01 14:54 +0000

csiph-web