python : 动态不刷屏幕输出 python/shell 实现
前言
后台运行程序有一种需求,比如查看当前进度,想在终端看到某个值的变化情况:
先提供一种很土的办法,把进度落地文件为 例如 process,采用创建写的方式。然后可以使用watch -n 1 cat process来查看进度。
这里提供两种方式,python和shell
shell版本,如下(附带一个进度条的例子)
#! /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版本,如下(附一个多线程输出进度的例子)
基础用法示例
#! /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)
子线程完成自己的任务,主线程跟踪执行进度。
** 小编推荐一个学python的学习qun 740322234
无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!
**
#! /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()
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
在编写python代码时,小白最容易犯的十几个错误 !
1).忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)该错误将发生在类似如下代码中: if spam== 42 print('Hello!') 2) 使用 = 而不是 ==(导致“SyntaxError: invalid syntax”)= 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中: if spam= 42: print('Hello!') 3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”) 记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中: print('Hello!') print('Howdy!') 或者:...
- 下一篇
var,let和const深入解析(二)
你想在在变量声明之前就使用变量?以后再也别这样做了。 新的声明方式(let,const)较之之前的声明方式(var),还有一个区别,就是新的方式不允许在变量声明之前就使用该变量,但是var是可以得。请看下面的代码,下面这个代码是可以正常运行的: function func() { console.log(localVariable); // undefined var localVariable = 5; console.log(localVariable); // 5 } func(); 但是这种却不可以 function func() { console.log(localVariable); // ReferenceError: localVariable is not defined let localVariable = 10; console.log(localVariable); // 10 } func(); 等下,我们上一章曾经介绍了一个叫“提升”的概念,它会吧所有的变量定义在作用域的最前面。这是否意味着如果我不在实际的定义之前使用变量,然后就不会有提升了呢?答案是否...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果