简体   繁体   中英

Change python print flush rate

Is it possible to change the rate that Python flushes content to the console? I know it is possible to add flush = True to the print command (at least in latest version of Python 3) to ensure the content is printed, however, is there a global setting that can be changed so that content is committed to the screen more often, or after a set amount of time.

(essentially, i want to make content be flushed more quickly, or at least in periodic intervals, but not every single print statement which would slow down the code).

You could do something like this toy example:

import time

print_interval = 1 # 1 second
last_print_time = time.time()

for i in range(100):
    time.sleep(0.1) # An arbitrary time-consuming task
    now = time.time()
    if now - last_print_time >= print_interval:
        print('something', flush=True)
        last_print_time = now
    else:
        print('somethine else', flush=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM