我有以下代码,我正在等待方法调用以返回响应。此响应可能需要 10 到 15 秒以上。我需要在用户等待方法返回响应时显示进度消息。我的 while 条件似乎没有显示进度条。response = my_api.execute("get some data")# There is a 10-15 minute delay here...i = 0while response is None: sys.stdout.write('\r') sys.stdout.write("Fetching MO attribute [%-20s] %d%%" % ('=' * i, 5 * i)) sys.stdout.flush() sleep(0.25) i += 1if response.get_output() != "1 instance(s)": raise MyError('Could not fetch data. )for line in response.get_output(): # Do the actual processing
2 回答


呼如林
TA贡献1798条经验 获得超3个赞
您需要线程,一个用于主后台进程,一个用于显示进程:
import sys
import time
import threading
import itertools
done = False
response = None
def animate():
i = 0
while response is None:
sys.stdout.write('\r')
sys.stdout.write("Fetching MO attribute [%-20s] %d%%" % ('=' * i, 5 * i))
sys.stdout.flush()
time.sleep(0.25)
i += 1
t = threading.Thread(target=animate)
t.start()
# background process
response = my_api.execute("get some data")
done = True
添加回答
举报
0/150
提交
取消