Standard output has some buffer, but it is blocked when it is filled. You may come across situations where you want to give up output as much as blocking. In such a case this
import fcntl, sys, os
fl = fcntl.fcntl(sys.stdout.fileno(), fcntl.F_GETFL)
fl = fl | os.O_NONBLOCK
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, fl)
By doing this
while True:
try:
print("hoge" * 100)
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
When I wrote
$ ./hoge.py | ./Slow program
I/O error(11): Resource temporarily unavailable
I/O error(11): Resource temporarily unavailable
...
It will behave like
Recommended Posts