Discussion

Tell HN: Python's print function is thread-unsafe

Some days ago I found that Python's print function is by default thread-unsafe, as the underlying sys.stdout is by default a TextIOWrapper, which is not thread safe. That means, for the code below

  from threading import Thread
  def target(x):
      for _ in range(100000): print(x)
  Thread(target=target, args=['a' * 10]).start()
  Thread(target=target, args=['b' * 10]).start()
Python could print out not only interleaved bytes (aaabbbabab), but also null bytes and uninitialized memory, thanks to non-synchronized buffering.
13 points kerneloops 2 years ago 6 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton