2016-08-04 239 views
6

我试图运行一个脚本,试图通过木偶管理在centos7系统上安装模块。 我想要在运行脚本时执行安装过程的进度条。 我正在使用tqdm模块来做到这一点。 这是一个如何我已经实现了模块瞬间:)蟒蛇进度条使用tqdm不停留在一条线上

from tqdm import tqdm 
for i in tqdm(commands): 
    res = run_apply(i) 

这里run_apply(是实际处理运行和应用的木偶配置的功能。

到目前为止,我得到了一个进度条,但是当执行消息写入控制台时,它一直向下移动控制台。 但是,我需要进度条在控制台底部保持不变并动态更新,而不会出现运行消息干扰条形图的情况。 我希望控制台上的执行相关消息按需要继续运行,但进度栏应该从开始到结束执行时保持在底部。

下面是我所看到的:

 File line: 0.00 
      Package: 0.05 
      Service: 0.19 
      File: 0.23 
      Exec: 0.23 
     Last run: 1470308227 
    Config retrieval: 3.90 
      Total: 4.60 
Version: 
      Config: 1470308220 
      Puppet: 3.7.3 
now here x 
result: 2 
38%|█████████████████████████████████████▋               | 5/13 [00:29<00:51, 6.44s/it]about to: profiles::install::download_packages 
about to run puppet apply --summarize --detailed-exitcodes --certname puppet -e "include profiles::install::download_packages" 
Error: Could not find class profiles::install::download_packages for puppet on node puppet 
Error: Could not find class profiles::install::download_packages for puppet on node puppet 
now here x 
result: 1 
46%|█████████████████████████████████████████████▏             | 6/13 [00:32<00:36, 5.27s/it]about to: profiles::install::install 
about to run puppet apply --summarize --detailed-exitcodes --certname puppet -e "include profiles::install::install" 
Error: Could not find class profiles::install::install for puppet on node puppet 
Error: Could not find class profiles::install::install for puppet on node puppet 
now here x 
result: 1 
54%|████████████████████████████████████████████████████▊            | 7/13 [00:34<00:26, 4.45s/it]about to: stx_network 
about to run puppet apply --summarize --detailed-exitcodes --certname puppet -e "include stx_network" 
Notice: Compiled catalog for puppet in environment production in 0.84 seconds 
Notice: /Stage[main]/Stx_network/Tidy[purge unused nics]: Tidying File[/etc/sysconfig/network-scripts/ifcfg-lo] 
... 

请让我知道我可以实现我想要什么。

回答

8

对于要在进度条上方打印的消息,需要向tqdm发信号表明您正在打印消息(否则tqdm,或任何其他进度条,无法知道您正在输出消息在进度栏旁边)。

为此,您可以使用tqdm.write(msg)而不是print(msg)来打印您的消息。如果您不想修改run_apply()以使用tqdm.write(msg)而不是print(msg),则可以使用redirect all standard output through tqdm from the toplevel script as described here

-1

尝试使用: 进度

import Progressbar 
progress = progressbar.ProgressBar() 
for i in progress(range(30)): 
    time.sleep(0.1) 

它看起来就像这样: 43%(30 13)| ################### ######### |已用时间:0:00:01 ETA:0:00:01

+0

以下是链接:https://pypi.python.org/pypi ?%3Aaction=search&term=Progressbar&submit=search –

+0

这真不幸赢了' t解决了这个问题,因为消息将被打印在进度条上,因此输出将与OP中一样出现乱码。 – gaborous

+0

如何......告诉我...... –