c z
c z

Reputation: 8987

Increase Python's stdout buffer size

Is there a way to increase the stdout buffer size from 8182 in Python or to delay the flush until I actually call flush?

Things I've tried that don't work:

Rationale: The aim here is to reduce console flickering. Buffering everything, as per this question indeed works, for instance when I try in C or by using the Windows API in Python, but the 8182 limit in Python seems to be causing problems that I can't get around on Unix.

Upvotes: 6

Views: 7710

Answers (2)

c z
c z

Reputation: 8987

Found the answer, actually very simple:

my_stdout = open( 1, "w", buffering = 100000 )
  • 1 is the fileno for stdout.
  • sys.stdout = my_stdout can be used to make the change to the default print target.
  • I've only tested this on Unix.

Upvotes: 6

blhsing
blhsing

Reputation: 106618

You can wrap the raw stdout stream, available as sys.stdout.buffer, with a larger buffer size with io.BufferedWriter, and then wrap the resulting buffered binary stream as a buffered text stream with io.TextIOWrapper:

import io
import sys

sys.stdout = io.TextIOWrapper(io.BufferedWriter(sys.stdout.buffer, new_size))

Here's a demonstration of the effect of increasing the buffer size of stdout to 100000 so that it does not flush the two 10000-character-long print outputs until a manual flush is called:

import io
import sys
import time

print('Original buffer size:', io.DEFAULT_BUFFER_SIZE)
for large_buffer in False, True:
    if large_buffer:
        print('Increasing buffer size...')
        sys.stdout = io.TextIOWrapper(io.BufferedWriter(sys.stdout.buffer, 100000))
    for i in range(2):
        time.sleep(2)
        print(str(i * 2) * 10000)
        time.sleep(2)
        print(str(i * 2 + 1) *10000)
        print(f'Flush #{i + 1}')
        sys.stdout.flush()

Demo: https://repl.it/@blhsing/UnkemptGullibleDecompiler

Upvotes: 6

Related Questions