Use sys.stdout.write() instead of print.

Experimentally sys.stdout.write() seems to behave atomically, while
print definitely doesn't.