Turn off buffering

后端 未结 4 1605

Where is the buffer in this following ... and how do I turn it off?

I am writing out to stdout in a python program like so:

for line in sys.stdin:
           


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 17:20

    file.readlines() and for line in file have internal buffering which is not affected by -u option (see -u option note). Use

    while True:
       l=sys.stdin.readline()
       sys.stdout.write(l)
    

    instead.

    By the way, sys.stdout is line-buffered by default if it points to terminal and sys.stderr is unbuffered (see stdio buffering).

提交回复
热议问题