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: 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 Date: Mon, 11 Aug 2014 19:19:05 +0900 Subject: Re: Is print thread safe? To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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 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