我正在尝试运行一个脚本,通过puppet管理在CentOS7系统上安装模块。我想在运行脚本的同时实现安装进度条。我使用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]
...
请告诉我如何实现我的愿望。
run_apply()
中使用print()
(从示例输出中看起来是这样),最好的情况是进度条会创建一个新行,并且您将在终端上得到多个显示不同状态的相同进度条,或者最坏的情况是由于\r
控制字符而擦除当前行上的先前打印,该字符用于清除先前的进度条显示,但它可以擦除任何显示,包括用户的显示。 - gaborous