Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76101
| 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!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| 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.05; 'lines,': 0.07; 'skip:\\ 20': 0.07; "ain't": 0.09; 'ascii': 0.09; 'bug.': 0.09; 'calls.': 0.09; 'expected.': 0.09; 'spaces': 0.09; 'tests,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'thread': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; '2.7.2': 0.16; '>in': 0.16; '>the': 0.16; '__future__': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'mangled': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'ought': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simpson': 0.16; 'stdout': 0.16; 'sys.stdout': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'utterly': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'normally': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'basis,': 0.24; 'certainly': 0.24; 'documented': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'logging': 0.26; 'mention': 0.26; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'lines': 0.31; 'usually': 0.31; "d'aprano": 0.31; 'implicit': 0.31; 'linux.': 0.31; 'steven': 0.31; 'work:': 0.31; 'file': 0.32; 'thanks!': 0.32; 'alone': 0.33; 'older': 0.33; 'sense': 0.34; 'skip:_ 10': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; '5000': 0.36; 'described': 0.36; 'object,': 0.36; 'received:com.au': 0.36; 'skip:> 10': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'should': 0.36; 'received:211': 0.38; 'anything': 0.39; 'expect': 0.39; 'does': 0.39; 'enough': 0.39; 'even': 0.60; 'middle': 0.60; 'most': 0.60; 'free': 0.61; 'simple': 0.61; 'times': 0.62; 'content-disposition:inline': 0.62; 'kind': 0.63; 'personal': 0.63; 'taking': 0.65; 'believe': 0.68; '>from': 0.68; 'promise': 0.68; 'safe': 0.72; 'demonstrates': 0.84; 'each,': 0.84; 'fifty': 0.84; 'safe.': 0.84; 'system!': 0.84; 'sorry.': 0.91 |
| Date | Tue, 12 Aug 2014 14:31:04 +1000 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
| Subject | Re: Is print thread safe? |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii; format=flowed |
| Content-Disposition | inline |
| In-Reply-To | <53e9583b$0$29973$c3e8da3$5496439d@news.astraweb.com> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| References | <53e9583b$0$29973$c3e8da3$5496439d@news.astraweb.com> |
| X-Optus-CM-Score | 0 |
| X-Optus-CM-Analysis | v=2.1 cv=BdjhjNd2 c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=PzczSypyshMA:10 a=gyjTKktfeJ0A:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=kZ7UWmmPAAAA:8 a=qszkbd2sRh_Nse5HgTkA:9 a=CjuIK1q_8ugA:10 a=pyH5b1fOeEsA:10 |
| 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 <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.12880.1407826485.18130.python-list@python.org> (permalink) |
| Lines | 88 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1407826485 news.xs4all.nl 2841 [2001:888:2000:d::a6]:55262 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:76101 |
Show key headers only | View raw
On 12Aug2014 09:56, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: >Cameron Simpson wrote: >> On 12Aug2014 02:07, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: >>>Is this documented somewhere? >> >> In python/2.7.6/reference/simple_stmts.html#index-22, "print" is described >> in terms of a "write" for each object, and a "write" for the separators. >> There is no mention of locking. > >Ah, thanks! > >> On that basis, I would find the interleaving described normal and >> expected. And certainly not "broken". > >I personally didn't describe it as "broken", Yes, sorry. >but it is, despite the >documentation. I just ran a couple of trials where I collected the output >of sys.stdout while 50 threads blasted "Spam ABCD EFGH" (plus the implicit >newline) to stdout as fast as possible using print. The result was that out >of 248165 lines[1], 595 were mangled. Many of the mangled lines were the >expected simple run-ons: > > Spam ABCD EFGHSpam ABCD EFGH\n\n > >which makes sense given the documentation, but there were lots of anomalies. > >Mysterious spaces appearing in the strings: > > Spam ABCD EFGH Spam ABCD EFGH\n\n > Spam ABCD EFGH Spam ABCD EFGH\n Spam ABCD EFGH\n > >occasional collisions mid-string: > > Spam ABSpam ABCD EFGH\nCD EFGH\n > >letters disappearing: > > Spam AB\nD EFGH\n > >and at least one utterly perplexing (to me) block of ASCII NULs appearing in >the middle of the output: > > \x00\x00\x00...\x00\x00\n > >This is with Python 2.7.2 on Linux. Sounds like print is not thread safe. Which it does not promise to be. But I would normally expect most file.write methods to be thread safe. Naively. >> Just use a lock! And rebind "print"! Or use the logging system! > >Personally, I believe that print ought to do its own locking. I don't, but I kind of believe "file"s should have thread safe write calls. Again, not guarrenteed AFAIR. >And print is a >statement, although in this case there's no need to support anything older >than 2.6, so something like this ought to work: > >from __future__ import print_function > >_print = print >_rlock = threading.RLock() >def print(*args, **kwargs): > with _rlock: > _print(*args, **kwargs) > >Sadly, using print as a function alone isn't enough to fix this problem, but >in my quick tests, using locking as above does fix it, and with no >appreciable slowdown. I would expect file.write to be fast enough that the lock would usually be free. With no evidence, just personal expectation. Taking a free lock should be almost instant. >[1] Even the number of lines of output demonstrates a bug. I had fifty >threads printing 5000 times each, which makes 250000 lines, not 248165. Sounds like the file internals are unsafe. Ugh. Cheers, Cameron Simpson <cs@zip.com.au> If it ain't broken, keep playing with it.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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