Python stdout, \r progress bar and sshd with Putty not updating regularly
I have a dead simple progress "bar" usi开发者_Python百科ng something like the following:
import sys
from time import sleep
current = 0
limit = 50
while current <= limit:
sys.stdout.write('\rSynced %s/%s orders' % (current, limit))
current_order += 1
sleep(1)
Works fine, except over ssh with Putty. Putty only updates every 3 minutes or if a line ends with \n. Is this a Putty setting, sshd_config, or can I code around it?
Try doing sys.stdout.flush()
after sys.stdout.write
call.
You can use flush()
to force an update.
sys.stdout.write('\r[%s%s]' % ('=' * completed, ' ' * (total-completed)))
sys.stdout.flush()
Use sys.stderr.write
instead, which is not buffered as sys.stdout
is, and this way you separate progress indicator from the (presumably) useful process output.
精彩评论