https://github.com/david-lor/python-wait4it
Python module to wait for a TCP port to be available
https://github.com/david-lor/python-wait4it
python python-wait4it retries wait-for-it wait4it waitforit
Last synced: 6 months ago
JSON representation
Python module to wait for a TCP port to be available
- Host: GitHub
- URL: https://github.com/david-lor/python-wait4it
- Owner: David-Lor
- License: isc
- Created: 2020-05-28T17:35:44.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-01T03:10:24.000Z (almost 2 years ago)
- Last Synced: 2025-04-24T03:46:53.260Z (6 months ago)
- Topics: python, python-wait4it, retries, wait-for-it, wait4it, waitforit
- Language: Python
- Homepage: https://pypi.org/project/wait4it/
- Size: 22.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# python-wait4it

[](https://pypi.org/project/wait4it/)
[](https://github.com/David-Lor/python-wait4it/blob/master/LICENSE.md)Wait-For-It Python module, that waits until a certain TCP port is available.
Based on the idea behind the well-known [wait-for-it](https://github.com/vishnubob/wait-for-it) script,
but created mainly as a Python package to be used on other Python applications, services or modules,
instead of being mainly a CLI tool.## Installing
Package is available at [PyPi](https://pypi.org/project/wait4it), so you can install it with `pip install wait4it` -
or from sources with `python setup.py install`.## Usage
```python
from wait4it import wait_for, WaitForTimeoutError# This should return instantly (if you have connection)
wait_for(host="google.com", port=80)# This should fail in 5 seconds
try:
wait_for(host="google.com", port=12345, timeout=5)
except TimeoutError:
# Actually will raise custom WaitForTimeoutError exception, but inherits from TimeoutError (except on Python2)
print("Failed! (as expected)")# This should return False in 15 seconds
wait_for(host="google.com", port=12345, raise_error=False)# Normally you will want to check for a port in localhost (e.g. a MySQL/MariaDB database).
# This can be done directly like:
wait_for(3306)# The exceptions include the failing host/port
try:
wait_for(host="google.com", port=12345)
except WaitForTimeoutError as ex:
assert ex.host == "google.com"
assert ex.port == 12345
```### wait_for_pass decorator
It works similarly to wait_for, but if the considered exceptions are raised on the decorated function, it will re-run
until it runs without raising these errors, or until the given retries limit is reached.The following example will randomly raise ZeroDivisionError in the function `divide_by_random`, which runs 10 times.
If fails more than twice, the exception will be thrown outside the function.```python
from random import randint
from wait4it import wait_for_pass@wait_for_pass(ZeroDivisionError, retries=2)
def divide_by_random(n=10):
d = randint(0, 1)
print("Gonna divide", n, "/", d)
return n / dfor _ in range(10):
r = divide_by_random()
print("Got result:", r)
```If `retries` is set to 0, the function will run forever until it passes without raising exceptions.
wait_for_pass also allows a parameter `retries_delay`, which can be used to define a delay, in seconds, between failed function executions.
## Dependencies & Compatibility
Not external dependencies are required. Compatible (tested with) Python 2.7, 3.4, 3.5, 3.6, 3.7, 3.8 - under Linux.
## Changelog
- 0.2.1 - Add retries_delay parameter to wait_for_pass
- 0.1.2 - Fix wait_for_pass decorator not looping indefinitely when retries=0
- 0.1.1 - Add wait_for_pass decorator
- 0.0.1 - Initial release## TODO
- Set retries limit on wait_for
- Set timeout on wait_for_pass