因为开发需求,需要把一个脚本放到服务器上跑,但是如果不加输出的话很难判断还有多长时间才能跑完,因此需要进度条:
from tqdm import tqdm
from io import BytesIO
from concurrent.futures import ThreadPoolExecutor, as_completed
file_list = [1,2,3,4,5]
thread_list = []
thread_pool = ThreadPoolExecutor(max_workers=10)
for each_file in file_list:
t = thread_pool.submit(self.getFeature, metaclass, each_file, feature_type)
thread_list.append(t)
for res in tqdm(as_completed(thread_list), total=len(thread_list)):
res.result()
这里是通过tqdm的包来实现实时进度条的效果,其中相当于是采用了多线程来实现,
其中self.getFeature是我用来执行主要任务的函数名,后面跟着的是函数需要的参数
简单一点的写法:
files_list=[1,2,3,4,5,6,7,8,9,10]
total_len=len(files_list)
for file_path in files_list:
count+=1
progress=int(count/total_len*100)
print("\r", end="")
print("进度: {}%: ".format(progress), "▓" * (progress // 2), end="")
sys.stdout.flush()
在服务器上运行脚本的时候还需要注意一点,就是如果是长时间跑的任务的话需要用
tmux来跑,tmux相当于新开一个独立的ssh会话,这样当前跑的任务不会因为你断掉ssh会话而结束
tmux进入:
tmux
展示所有对话
tmux ls
进入某个tmux对话
tmux attach -t xx