RejeeshChandran
RejeeshChandran

Reputation: 4358

Better way to write a polling function in python

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?

Upvotes: 9

Views: 41775

Answers (1)

Sandeep Hukku
Sandeep Hukku

Reputation: 399

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.

Upvotes: 14

Related Questions