Is print thread safe?

Cameron Simpson cs at zip.com.au
Mon Aug 11 17:53:11 EDT 2014


On 12Aug2014 02:07, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>INADA Naoki wrote:
>
>> On Python 3, print is thread safe.
>> But Python 2 has broken scenario:
>
>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.

On that basis, I would find the interleaving described normal and expected. And 
certainly not "broken".

Just use a lock! And rebind "print"! Or use the logging system!

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

Wow!  Yet another place that I've been quoted...
         - Andy Beals <bandy at cinnamon.com>



More information about the Python-list mailing list