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


Groups > comp.lang.python > #76101

Re: Is print thread safe?

Date 2014-08-12 14:31 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: Is print thread safe?
References <53e9583b$0$29973$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.12880.1407826485.18130.python-list@python.org> (permalink)

Show all headers | 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 | 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