Is print thread safe?

Marko Rauhamaa marko at pacujo.net
Tue Aug 12 01:01:39 EDT 2014


Steven D'Aprano <steve+comp.lang.python at 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?


Marko



More information about the Python-list mailing list