from multiprocessing.pool import ThreadPool
import time
def progress_bar(total, ncols=10, desc='Processing', unit='it'):
def get_bar(l, n, ncol):
r = ncol - l - 1
bar = ''.join(['#' for i in range(l)])
if l != ncol:
bar += f'{n}'
bar += ''.join([' ' for i in range(r)])
return '|'+bar+'|'
processing = f'{0:>3d}%'
bar = get_bar(0, 0, ncols)
mod = f'{desc}: {bar} {processing:>4s}'
print('\r'+mod, end='')
for i in range(total):
start_time = time.time()
# 内容处理
time.sleep(0.1)
stop_time = time.time()
delta_time = stop_time - start_time
temp = (i+1)/total
processing = f'{int(temp*100):>3d}%'
l = int(temp*ncols)
n = int(temp*ncols*10)%10
bar = get_bar(l, n, ncols)
mod = f'{desc}: {bar} {processing:>4s} {1/delta_time:.2f}it/s'
print('\r'+mod, end='')
print('\n', end='')
def main():
print('main start')
pool = ThreadPool(5)
for i in range(5):
pool.apply_async(progress_bar, (100,50))
time.sleep(1)
pool.close()
pool.join()
print('main over')
main()
读者好,代码如上,我想写个简单的类似tqdm的进度条展示程序。