Better way to write a polling function in python

Question:

I wrote a polling function to check the value of reg_result variable for 120 seconds.

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)

Is there any other better way of writing a polling method?

Is it possible by not using a while loop?

Asked By: RejeeshChandran

||

Answers:

There is a library in python Polling(https://pypi.python.org/pypi/polling/0.3.0)
You can use this

from polling import TimeoutException, poll
try:
    poll(lambda: reg_result=='REGISTERED', timeout=120, step=1)
except TimeoutException as tee:
    print "Value was not registered"

Hope it helps.

Answered By: Sandeep Hukku
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.