我写了一个轮询函数来检查 reg_result
变量的值 120 秒。
reg_result = 0
while_timeout = time.time() + 120
while reg_result is not "REGISTERED" and time.time() < while_timeout:
reg_result = LeshanObj.validate_reg_time(parameter_1)
还有其他更好的方式来编写轮询方法吗?
是否可以不使用 while
循环?
最佳答案
python Polling(https://pypi.python.org/pypi/polling/0.3.0)中有一个库 你可以用这个
from polling import TimeoutException, poll
try:
poll(lambda: reg_result=='REGISTERED', timeout=120, step=1)
except TimeoutException as tee:
print "Value was not registered"
希望对您有所帮助。
关于python - 在 python 中编写轮询函数的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41713234/