python: dynamic output without screen swiping

Posted by samsolomonraj on Fri, 06 Dec 2019 04:06:18 +0100

Preface
There is a requirement for background running programs, such as viewing the current progress, to see the change of a certain value at the terminal:

First, it provides a very local way to create and write progress landing files such as process. You can then use watch -n 1 cat process to see the progress.

There are two ways, python and shell

shell version, as follows (with an example of a progress bar)

#! /bin/bash

for ((i=0; $i<=100; i+=1))
do
    printf "progress: [%-100s] %d%%\r" "xxxxxxxxxx xxx xxx" $i
    sleep 1
done
function sleepPrograss(){
    [ $# -eq 0 ] && echo "sleepPrograss Usage: sleepPrograss 10 "
    [ $# -eq 0 ] && return 1


    allTime=$1
    strDone=''
    stepTime=$(echo "scale=1; $allTime/100" | bc)
    for ((i=0; $i<=100; i+=1))
    do
        printf "progress: [%-100s] %d%%\r" $strDone $i
        sleep $stepTime 
        strDone+='#'
    done
    echo
}

python version, as follows (with an example of multithreaded output progress)

Basic usage example

#! /usr/bin/env python

import os
import sys
import time
while 1:
    os.write(1, "\r[%.3f]" % time.time())
    sys.stdout.flush()
    time.sleep(1)

The sub thread completes its own tasks, and the main thread tracks the execution progress.

**Xiaobian recommends a study of python qun 740322234
Whether you are Daniel or Xiaobai, want to change careers or want to join the industry, you can come to understand and learn together! There are development tools in the skirt, many dry goods and technical information sharing!

**

#! /usr/bin/env python 
# -*- coding: utf-8 -*-

'''
@filename :   demo.py
@authors  :   U{peterguo<mailto: peterguo@tencent.com>}
@copyright:   tencent
@date     :   2012-11-15
@version  :   1.0.0.1
'''
import os
import sys
import time
import thread

g_nTotal = 100
g_nProcessed = 0
g_fStartTime = time.time()

def simpleThdreadFun(interval):
    global g_nProcessed
    global g_nTotal
    while g_nTotal > g_nProcessed:
        time.sleep(interval)
        g_nProcessed += 1
    thread.exit_thread()

def test():
    
    global g_nTotal
    global g_nProcessed
    global g_fStartTime
    g_fStartTime = time.time()
    
    thread.start_new_thread(simpleThdreadFun, (1,))
    thread.start_new_thread(simpleThdreadFun, (2,))
    thread.start_new_thread(simpleThdreadFun, (3,))
    thread.start_new_thread(simpleThdreadFun, (4,))
    
    while True:
        time.sleep(0.5)
        fRunTime = time.time() - g_fStartTime
        nLeftNum = g_nTotal - g_nProcessed
        fLeftTime = fRunTime * nLeftNum / (g_nProcessed + 0.1)
        fPrograss = 100.0 * g_nProcessed / g_nTotal
        
        os.write(1, "\rLeftTime[%.3f]\tLeftNum[%d]\tProgress[%.3f %% (%d/%d) ]" %
                   (fLeftTime, nLeftNum, fPrograss, g_nProcessed, g_nTotal))
        sys.stdout.flush()
        if g_nTotal <= g_nProcessed:
            break
    print "\nTest Done, use %.3f seconds" % (time.time() - g_fStartTime)
      
if __name__=='__main__':
    test()

Topics: Python shell