Skip to main content

Powerful polling utility with many configurable options

Project description

Build Status PyPI PyPI


Polling2 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

Polling2 is a fork of the original polling. It was forked when the original maintainer failed to respond to issues or PRs.


pip install polling2

Development installation

# install lib, but use system links from the repo into sitepackages.
python develop
# install test dependenices.
python test
# run the tests
pytest tests


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

import requests
    lambda: requests.get('').status_code == 200,

If you are creating a new cloud provider instance (e.g. waiting for an EC2 instance to come online), you can continue to poll despite getting ConnectionErrors:

import requests
    lambda: requests.get('your.instance.ip').status_code == 200,

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 elapsed
file_handle = polling2.poll(
    lambda: open('/tmp/myfile.txt'),

# Polling will return the value of your polling function, so you can now interact with it

Example: Polling for Selenium WebDriver elements

from selenium import webdriver
driver = webdriver.Firefox()

search_box = polling2.poll(
    lambda: driver.find_element_by_id('search'),

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.

import random
    polling2.poll(lambda: random.choice([0, (), False]), step=0.5, timeout=1)
except polling2.TimeoutException, te:
    while not te.values.empty():
        # Print all of the values that did not meet the exception
        print te.values.get()

Example: Using a custom condition callback function

import requests

def is_correct_response(response):
    """Check that the response returned 'success'"""
    return response == 'success'

    lambda: requests.put('', data={'username': 'Jill'},

Release notes


  • Remove test-only dependencies from requirements to install the package.
  • No longer testing on python 2.6. Add support for travis testing on python 3.6 and pypy 3.5.
  • Creation of polling2, forked from polling as previous maintainer seems to be ignoring issues and pull-requests.
  • Remove *a, **k from poll signature. This allows Type errors to be raised if caller spells arguments into correctly, making bugs easier to find.


  • Support Python 3.4+


  • 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


  • First version


  • Justin Iso (original creator)
  • Donal Mee

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for polling2, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size polling2-0.4.0-py2-none-any.whl (6.0 kB) File type Wheel Python version py2 Upload date Hashes View
Filename, size polling2-0.4.0.tar.gz (6.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page