How can I do an atomic write to stdout in python?
问题 I've read in some sources that the print command is not thread-safe and the workaround is to use sys.stdout.write command instead, but still it doesn't work for me and the writing to the STDOUT isn't atomic. Here's a short example (called this file parallelExperiment.py): import os import sys from multiprocessing import Pool def output(msg): msg = '%s%s' % (msg, os.linesep) sys.stdout.write(msg) def func(input): output(u'pid:%d got input \"%s\"' % (os.getpid(), str(input))) def