python - 在函数调用上使用 tqdm 进度条
问题描述
有没有办法在函数调用上使用 tqdm,因为我不了解如何显示进度条,因为我没有任何循环:这是函数调用发生的地方:
if length == 2:
post().request(email,password)
每次它发出请求时,我都希望我的进度条移动,有什么办法吗?
解决方案
所以post是一个类。据我了解你的问题,你可以试试这些:
对于每个请求的进度条:
class post():
def __init__(self):
pass
def req(self):
for _ in trange(1):
print("req")
p = post()
p.req()
对于 n_req 请求的单个进度条:
class post(tqdm.tqdm):
def __init__(self, n_req = 1):
tqdm.tqdm.__init__(self)
self.total = n_req
def req(self, i):
print("req", i)
self.update()
n_req = 5
with post(n_req) as p:
for i in range(n_req):
p.req(i)
#------------OR-----------------
n_req = 5
with post(n_req) as p:
p.req(1); p.req(2); p.req(3); p.req(4); p.req(5)
(我不确定为什么它在没有with
格式的情况下无法工作。可能是因为 python 对象或回调与 c++ 对象的工作方式不同。)