Ste*_*e R 4 python black-box python-3.x progress-bar
关于 Python 脚本执行时终端中进度条的显示存在许多现有问题,但每个问题都基于执行操作然后更新进度图形的循环。
不幸的是,我想要显示其进度的函数(或者至少是一个旋转器对象来显示它正在工作)是一个我无法(至少真的、真的不应该)改变的黑匣子。本质上,我想做的是:
#pseudocode input
print('Loading')
spinner.begin()
blackbox() #a few thousand operations happen in here
spinner.end()
print('Finished')
#pseudocode output
Loading.
Loading..
Loading...
Loading.
Loading..
Loading...
Finished
Run Code Online (Sandbox Code Playgroud)
尽管理想情况下,这将是省略号的动画,而不是打印多行。在我开始构建愚蠢的 ASCII 动画之前,有一个主要障碍:
有没有办法一边跑spinner一边blackbox()跑?或者,是否有一种黑客可以暂停blackbox(),无论其内容如何,每隔几百毫秒更新旋转器图形,然后从中断处恢复?
我已经用进度模块尝试过这个,但没有运气......我什至无法让示例代码工作,它只是在我开始迭代后挂起,直到我按 Ctrl+C 退出。
我喜欢用alive_progress这个。
from typing import ContextManager, Optional
from alive_progress import alive_bar
def spinner(title: Optional[str] = None) -> ContextManager:
"""
Context manager to display a spinner while a long-running process is running.
Usage:
with spinner("Fetching data..."):
fetch_data()
Args:
title: The title of the spinner. If None, no title will be displayed.
"""
return alive_bar(monitor=None, stats=None, title=title)
Run Code Online (Sandbox Code Playgroud)
安装:pip install alive-progress
| 归档时间: |
|
| 查看次数: |
4225 次 |
| 最近记录: |