Today I faced a problem where I had to print to the standard output in a synchronized way. I put the following code in a file named rpython.py
from __future__ import print_function from threading import Lock _global_lock = Lock() _old_print = print def print(*a, **b): with _global_lock: _old_print(*a, **b)Now, in every Python module that requires synchronized printing I typed:
from __future__ import print_function from rpython import printAlternatively, if you don't want to shadow the default printing you can type:
from __future__ import print_function from rpython import print as rprintHave a nice, synchronized day.
EDIT:
I should have started with a clear statement that Python's print is not synchronized in any way. Multiple threads writing concurrently to any stream can interfere and usually they do that, so as an effect you get a random mixture of massages which looks really ugly.