python - 在 python 中编写轮询函数的更好方法

标签 python python-3.x python-2.x

我写了一个轮询函数来检查 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/

相关文章:

Python 3.4 计算众数、读取文件的中位数

python - 如何从我收到的电子邮件中的超链接中提取 URL?

python - 如何使用反斜杠 x\x 代码解码 ascii 字符串

python - 优化器最小化错误: 'float' object has no attribute 'dtype'

python - 有没有更好的方法来确定某些东西是 python 中的模块

python - 如何从外部强制正在运行的应用程序执行基本任务?

python - 从标准输入读取二进制数据

python - 提示用户输入()并使用正则表达式

python - 猜数字游戏-全局变量

python - 如何在同一行打印 2 个项目