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


Groups > comp.lang.python > #76100

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!newsfeed3a.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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'handler': 0.05; 'tries': 0.07; 'runs': 0.10; 'python': 0.11; 'def': 0.12; "wouldn't": 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; '__future__': 0.16; 'c-level': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'handler.': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'ought': 0.16; 'personally,': 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; 'wrote:': 0.18; 'later': 0.20; 'import': 0.22; 'putting': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'issue,': 0.24; 'cheers,': 0.24; 'header:In-Reply-To:1': 0.27; 'host': 0.29; 'work.': 0.31; 'code': 0.31; 'context.': 0.31; "d'aprano": 0.31; 'ordinary': 0.31; 'work:': 0.31; 'older': 0.33; 'plain': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'received:com.au': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'level': 0.37; 'received:211': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'signal': 0.60; 'course': 0.61; 'content-disposition:inline': 0.62; '(that': 0.65; 'close': 0.67; 'believe': 0.68; 'statement,': 0.68; 'prone': 0.91; 'imagine': 0.93
Date Tue, 12 Aug 2014 16:15:20 +1000
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
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 <87k36ee1xo.fsf@elektro.pacujo.net>
User-Agent Mutt/1.5.21 (2010-09-15)
References <87k36ee1xo.fsf@elektro.pacujo.net>
X-Optus-CM-Score 0
X-Optus-CM-Analysis v=2.1 cv=fvDlOjIf 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=KaybevnVAAAA:8 a=kZ7UWmmPAAAA:8 a=6qxQwj5dJAuZdegWAz4A:9 a=CjuIK1q_8ugA:10 a=X5q3X6-Q1Y0A:10 a=pyH5b1fOeEsA:10
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.12879.1407824124.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1407824124 news.xs4all.nl 2883 [2001:888:2000:d::a6]:49133
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76100

Show key headers only | View raw


On 12Aug2014 08:01, Marko Rauhamaa <marko@pacujo.net> wrote:
>Steven D'Aprano <steve+comp.lang.python@pearwood.info>:
>> Personally, I believe that print ought to do its own locking. 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)
>
>Could this cause a deadlock if print were used in signal handlers?

At the C level one tries to do as little as possible in q signal handler.  
Typically setting a flag or putting something on a queue for later work.

In Python that may be a much smaller issue, since I imagine the handler runs in 
the ordinary course of interpretation, outside the C-level handler context.

I personally wouldn't care if this might deadlock in a handler (lots of things 
might; avoid as many things as possible). Also, the code above uses an RLock; 
less prone to deadlock than a plain mutex Lock.

Cheers,
Cameron Simpson <cs@zip.com.au>

A host is a host from coast to coast
& no one will talk to a host that's close
Unless the host (that isn't close)
is busy, hung or dead
         - David Lesher, wb8foz@skybridge.scl.cwru.edu

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