eprogress 是一個簡單、易用的基于Python3的命令行(terminal)進(jìn)度條庫,可以自由選擇使用單行顯示、多行顯示進(jìn)度條或轉(zhuǎn)圈加載方式,也可以混合使用。
作者:竹塵居士
?
pip install eprogress
easy_install eprogress
導(dǎo)入eprogress
from eprogress import LineProgress, CircleProgress, MultiProgressManager
實例化進(jìn)度條對象,更新進(jìn)度
# circle loading circle_progress = CircleProgress(title='circle loading') for i in range(1, 101): circle_progress.update(i) time.sleep(0.1)# line progress line_progress = LineProgress(title='line progress') for i in range(1, 101): line_progress.update(i) time.sleep(0.05)# multi line or circle loading progressprogress_manager = MultiProgressManager()progress_manager.put(str(1001), LineProgress(total=100, title='1 thread'))progress_manager.put(str(1002), LineProgress(total=100, title='2 thread'))progress_manager.put(str(1003), LineProgress(total=100, title='3 thread'))progress_manager.put(str(1004), CircleProgress(title='4 thread'))... ...progress_manager.update(key, progress)
圓形加載條使用update(progress)實例方法進(jìn)行刷新,只有當(dāng)參數(shù)大于0時才會轉(zhuǎn)動。
線性進(jìn)度條使包含4個可選參數(shù):
@param total : 進(jìn)度總數(shù)@param symbol : 進(jìn)度條符號@param width : 進(jìn)度條展示的長度@param title : 進(jìn)度條前面展示的文字
創(chuàng)建實例后調(diào)用update(progress)實例方法更新進(jìn)度。
多行進(jìn)度顯示使用MultiProgressManager類,實例化該類,調(diào)用put(key,progressBar)方法統(tǒng)一管理多個進(jìn)度條,內(nèi)部使用一個dict來收集進(jìn)度條,多行顯示的順序為put的順序。更新某個進(jìn)度條時使用progressMangager的update(key,progress)方法,該key為put進(jìn)度條時使用的key。
無論是使用多行進(jìn)度條混合還是使用單行進(jìn)度條,都不用考慮多線程更新的問題,內(nèi)部已用Lock()加鎖。