Skip to content

Repository files navigation

polling

Polling is a powerful python utility used to wait for a function to return a certain expected condition. Some possible uses cases include:

  • Wait for API response to return with code 200
  • Wait for a file to exist (or not exist)
  • Wait for a thread lock on a resource to expire

Installation

pip install polling

Examples

Example: Poll every minute until a url returns 200 status code

importrequestspolling.poll(
lambda: requests.get('http://google.com').status_code==200,
step=60,
poll_forever=True)

Example: Poll for a file to exist

# This call will wait until the file exists, checking every 0.1 seconds and stopping after 3 seconds have elapsedfile_handle=polling.poll(
lambda: open('/tmp/myfile.txt'),
ignore_exceptions=(IOError,),
timeout=3,
step=0.1)
# Polling will return the value of your polling function, so you can now interact with itfile_handle.close()

Example: Polling for Selenium WebDriver elements

fromseleniumimportwebdriverdriver=webdriver.Firefox()
driver.get('http://google.com')
search_box=polling.poll(
lambda: driver.find_element_by_id('search'),
step=0.5,
timeout=7)
search_box.send_keys('python polling')

Example: Using the polling timeout exception

# An exception will be raised by the polling function on timeout (or the maximum number of calls is exceeded).# This exception will have a 'values' attribute. This is a queue with all values that did not meet the condition.# You can access them in the except block.importrandomtry:
polling.poll(lambda: random.choice([0, (), False]), step=0.5, timeout=1)
exceptpolling.TimeoutException, te:
whilenotte.values.empty():
# Print all of the values that did not meet the exceptionprintte.values.get()

Example: Using a custom condition callback function

importrequestsdefis_correct_response(response):
"""Check that the response returned 'success'"""returnresponse=='success'polling.poll(
lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},
check_success=is_correct_response,
step=1,
timeout=10)

Release notes

0.2.0

  • Allow users to access a "last" attribute on the exceptions. This should hold the last evaluated value, which is the more common use case than getting the first value.
  • Fix a bug that actually ran 1 more time than value specified by max_tries

0.1.0

  • First version

About

Powerful polling utility in Python

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages