首页 > 解决方案 > 在 if 语句中使用 tqdm 进度条

问题描述

其实我有这个代码:

#!/usr/bin/env python3
import sys
import requests
import random
from multiprocessing.dummy import Pool
from pathlib import Path
requests.urllib3.disable_warnings()

print ('Give name of txt file on _listeNDD directory (without.txt)'),
file = str(input())

if Path('_listeNDD/'+file+'.txt').is_file():
    print ('--------------------------------------------------------')
    print ("Found")
    print ('--------------------------------------------------------')
    print ('Choose name for the output list (without .txt)'),
    nomRez = str(input())
    filename = '_listeNDD/'+file+'.txt'
    domains = [i.strip() for i in open(filename , mode='r').readlines()]
else:
    print ('--------------------------------------------------------')
    exit('No txt found with this name')


def check(domain):
    try:
        r = requests.get('https://'+domain+'/test', timeout=5, allow_redirects = False)
        if "[core]" in r.text:
            with open('_rez/'+nomRez+'.txt', "a+") as f:
                print('https://'+domain+'/test', file=f)
    except:pass

mp = Pool(100)
mp.map(check, domains)
mp.close()
mp.join()
exit('finished')

根文件的画面


使用此代码,它打开目录“_listeNDD”上的文本文件,我在目录“_rez”上写入新的文本文件。显然它对于十个元素来说非常快,但是当它变得更大时,我想要一个进度条来知道我是否有时间煮咖啡。

我曾亲自尝试使用 github tqdm,但不幸的是,它为它所做的每项工作显示了一个进度条,而我只想要一个用于所有工作的进度条......

任何想法?谢谢

编辑:使用这篇文章,我没有成功

if __name__ == '__main__':
   p = Pool(100)
   r = p.map(check, tqdm.tqdm(range(0, 30)))
   p.close()
   p.join()

我没有足够高的 python 级别来掌握这一点,所以我可能将它严重集成到我的代码中。我还看到:

if __name__ == '__main__':
   r = process_map(check, range(0, 30), max_workers=2)

标签: pythonpython-3.xlisttqdm

解决方案


推荐阅读