实现进度条功能

 

方法一:简单FOR实现打印进度条功能
for i in range(10):
    print("#",end="",flush=True)
    time.sleep(0.4)

 

#方法二,yeild实现复杂进度条功能

def show_process(total):
    recive_size=0
    current_size=0

    while recive_size<total:

        if int(recive_size/total*100) >current_size: #进度比现在的大
            print("#",end="",flush=True)

            current_size=int(recive_size/total*100)
        new_size=yield  #中断
        recive_size+=new_size

total=10000000
recive_size=0

precess=show_process(total) #调用getnerator
precess.__next__()
while recive_size <total:
    recive_size+=10
    try:
        precess.send(10) #给generotor发送数据,并继续yield后面的执行
    except StopIteration as e:
        print("100%")

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2023-03-21
  • 2021-07-23
  • 2021-09-07
  • 2021-06-02
  • 2021-11-07
  • 2022-01-11
猜你喜欢
  • 2022-12-23
  • 2021-09-14
  • 2021-08-13
  • 2021-08-22
  • 2022-12-23
相关资源
相似解决方案