用充分的技巧来编写 Python 中的投票功能

2024-05-06 19:30:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个轮询函数来检查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循环是否可能?


Tags: and方法函数parametertimeistimeoutnot
1条回答
网友
1楼 · 发布于 2024-05-06 19:30:09

python轮询中有一个库(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"

希望有帮助。

相关问题 更多 >