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


Groups > comp.lang.python > #76042

Re: Is print thread safe?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <songofacandy@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'broken': 0.04; 'mixed': 0.09; 'newline': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'thread': 0.14; 'spam"': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'this?': 0.23; 'mon,': 0.24; 'cc:2**0': 0.24; 'appreciated': 0.26; 'first,': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'lines': 0.31; '3.x': 0.31; "d'aprano": 0.31; 'prints': 0.31; 'steven': 0.31; 'url:python': 0.33; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; '2.6': 0.36; 'url:listinfo': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'two': 0.37; 'pm,': 0.38; 'does': 0.39; 'url:mail': 0.40; '"spam"': 0.84; 'safe.': 0.84; 'together,': 0.84; '4:44': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=ZpHiMHE3xNbblPN66RbeRAb2V87WMTZtHjqW/3QbHaU=; b=bhaheM9eVQolPfmJelRMkHEGHrv0U+dYDOyQx9UoC04pt9RuqKO3uHXKovLMtqc0q1 JZhCjtu98N+5mHZ5nXwc7koH/KEGmvJEVHoDoyM2k9i7dyU1HtTlGnU0jXOtTMqIA+wH ae1hdLwEc/Pq+9Z51IioNaxBZg1op933osPX3o2jMe8hrPEENWM2LgQdPRh484FQpGtm RlKDv9LjBEzING1Mx7wOznHZ/CLsLsLV/ARVfRJqr5xedU6YujP75EgDwZVrUTUB0Pyl kMixGkTbYkaT0IUOhX6cuJFRwkrxX7L0ZvjRdVrFdJKknzCYcBd/0uFV7IJ5kMvMKOrm vkAA==
X-Received by 10.140.22.137 with SMTP id 9mr16761936qgn.4.1407752366001; Mon, 11 Aug 2014 03:19:26 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <53e87451$0$29890$c3e8da3$5496439d@news.astraweb.com>
References <53e87451$0$29890$c3e8da3$5496439d@news.astraweb.com>
From INADA Naoki <songofacandy@gmail.com>
Date Mon, 11 Aug 2014 19:19:05 +0900
Subject Re: Is print thread safe?
To "Steven D'Aprano" <steve@pearwood.info>
Content-Type text/plain; charset=UTF-8
Cc "python-list@python.org" <python-list@python.org>
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12845.1407752374.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1407752374 news.xs4all.nl 2915 [2001:888:2000:d::a6]:40542
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76042

Show key headers only | View raw


On Python 3, print is thread safe.

But Python 2 has broken scenario:

print "spam", "spam", "spam"  # thread 1
print "eggs", "eggs", "eggs"  # thread 2

In this case, 2 lines are mixed.

In your case, "spam spam spam" and "eggs eggs eggs" are not mixed.
But newline is mixed like:

spam spam spameggs eggs eggs

eggs eggs eggsspam spam spam
eggs eggs eggs

spam spam spam


On Mon, Aug 11, 2014 at 4:44 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> Specifically for Python 2.6 and 2.7, but answers for 3.x appreciated as
> well.
>
> Is print thread safe? That is, if I have two threads that each call
> print, say:
>
> print "spam spam spam"  # thread 1
> print "eggs eggs eggs"  # thread 2
>
> I don't care which line prints first, but I do care if the two lines are
> mixed in together, something like this:
>
> spam spaeggs eggs m seggspams
>
>
> Does print perform its own locking to prevent this?
>
>
>
> --
> Steven
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
INADA Naoki  <songofacandy@gmail.com>

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


Thread

Is print thread safe? Steven D'Aprano <steve@pearwood.info> - 2014-08-11 07:44 +0000
  Re: Is print thread safe? INADA Naoki <songofacandy@gmail.com> - 2014-08-11 19:19 +0900
    Re: Is print thread safe? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-12 02:07 +1000
      Re: Is print thread safe? Cameron Simpson <cs@zip.com.au> - 2014-08-12 07:53 +1000
        Re: Is print thread safe? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-12 09:56 +1000
          Re: Is print thread safe? Chris Angelico <rosuav@gmail.com> - 2014-08-12 10:14 +1000
          Re: Is print thread safe? Marko Rauhamaa <marko@pacujo.net> - 2014-08-12 08:01 +0300
            Re: Is print thread safe? Cameron Simpson <cs@zip.com.au> - 2014-08-12 16:15 +1000
          Re: Is print thread safe? Cameron Simpson <cs@zip.com.au> - 2014-08-12 14:31 +1000
          Re: Is print thread safe? Chris Angelico <rosuav@gmail.com> - 2014-08-12 23:53 +1000

csiph-web