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


Groups > comp.lang.python > #76100

Re: Is print thread safe?

Date 2014-08-12 16:15 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: Is print thread safe?
References <87k36ee1xo.fsf@elektro.pacujo.net>
Newsgroups comp.lang.python
Message-ID <mailman.12879.1407824124.18130.python-list@python.org> (permalink)

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