python基础-实现进度条功能,for和yield实现
实现进度条功能
方法一:简单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%")