Powerful polling utility with many configurable options
Project description
[](https://travis-ci.org/justiniso/polling)
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 requests
def 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.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
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 requests
def 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.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
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
polling-0.3.0.tar.gz
(5.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polling-0.3.0.tar.gz.
File metadata
- Download URL: polling-0.3.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0f1f804843fe78e09f6fc6de6fb8b11f73be0acce9cde262b18e4a7ad4005a6
|
|
| MD5 |
d8970fe28e8a0038f4ca954129b55bfc
|
|
| BLAKE2b-256 |
a142d11fca25b8ee038233bfabdd3dc89c92ce69f551fd9605093570bdfa7f89
|
File details
Details for the file polling-0.3.0-py3-none-any.whl.
File metadata
- Download URL: polling-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4896be05109fa0dead600e281d312446d01463962c1072d43b631f3ffd066c2
|
|
| MD5 |
e7fc7a7e79d89c59637f6df9ef9a57cd
|
|
| BLAKE2b-256 |
bfdac61da72dddeb52a8b4011c3d7fdbbee2b83e935ecd136f4b78c877defe70
|