Skip to main content

Updated polling utility with many configurable options

Project description

Build Status PyPI PyPI

polling2

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.

Installation

pip install polling2

Development installation

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

Examples

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

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

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
polling2.poll(
    lambda: requests.get('your.instance.ip').status_code == 200,
    step=60,
    ignore_exceptions=(requests.exceptions.ConnectionError,),
    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 elapsed
file_handle = polling2.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 it
file_handle.close()

Example: Polling for Selenium WebDriver elements

from selenium import webdriver
driver = webdriver.Firefox()

driver.get('http://google.com')
search_box = polling2.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.

import random
try:
    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'

polling2.poll(
    lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},
    check_success=is_correct_response,
    step=1,
    timeout=10)

Release notes

0.4.0

  • Fixed polling function from waiting another sleep whenever the max_tries value has reached zero.
  • 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.

0.3.0

  • Support Python 3.4+

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

Contributors

  • 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.

Source Distribution

polling2-0.4.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polling2-0.4.1-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file polling2-0.4.1.tar.gz.

File metadata

  • Download URL: polling2-0.4.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.13

File hashes

Hashes for polling2-0.4.1.tar.gz
Algorithm Hash digest
SHA256 bf064bef90dcd3a9e6ff17e8a0a27bbd6aa7ee464d3e9df93ae3aac397efff55
MD5 c7c669544dcb9aadc39717931294bddc
BLAKE2b-256 a69022d34c9231b6d63445ad1c413f470a042f1306a17759bd40f1fe495d95a5

See more details on using hashes here.

File details

Details for the file polling2-0.4.1-py2.py3-none-any.whl.

File metadata

  • Download URL: polling2-0.4.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.13

File hashes

Hashes for polling2-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ff1ee8484cda9f7f91a7ad80b83094cffd87c706b94009d06b54ee5976c1e123
MD5 836f3fe228df575268abf9d9a1d3095f
BLAKE2b-256 e0b08f2afc5e40b64c12dc9f92b5668811409c18ca102c7263b504388f2e0532

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page