Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/justiniso/polling
Powerful polling utility in Python
https://github.com/justiniso/polling
Last synced: 15 days ago
JSON representation
Powerful polling utility in Python
- Host: GitHub
- URL: https://github.com/justiniso/polling
- Owner: justiniso
- License: mit
- Created: 2015-04-08T03:52:24.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-05-22T19:48:59.000Z (over 3 years ago)
- Last Synced: 2024-10-01T10:13:09.247Z (about 1 month ago)
- Language: Python
- Size: 19.5 KB
- Stars: 139
- Watchers: 4
- Forks: 17
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/justiniso/polling.svg?branch=master)](https://travis-ci.org/justiniso/polling)
[![PyPI](https://img.shields.io/pypi/dm/polling.svg)]()
[![PyPI](https://img.shields.io/pypi/v/polling.svg)]()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
```python
import requests
polling.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:
```python
import requests
polling.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
```python
# This call will wait until the file exists, checking every 0.1 seconds and stopping after 3 seconds have elapsed
file_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 it
file_handle.close()
```
### Example: Polling for Selenium WebDriver elements```python
from selenium import webdriver
driver = 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
```python
# 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:
polling.poll(lambda: random.choice([0, (), False]), step=0.5, timeout=1)
except polling.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
```python
import requestsdef is_correct_response(response):
"""Check that the response returned 'success'"""
return response == '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.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