Skip to main content

python-wait4it

Test PyPi License

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 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, so you can install it with pip install wait4it - or from sources with python setup.py install.

Usage

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.

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 / d

for _ 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

Metadata

Release files for wait4it 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wait4it 0.2.1
File Size Uploaded
wait4it-0.2.1.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wait4it 0.2.1
File Interpreter ABI Platform
wait4it-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / wait4it-0.2.1.tar.gz

Download URL wait4it-0.2.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0ffd87a4b09a42a95b31b9f0cbe7865848d31e689baf7f743b41ce4e8c72e269
BLAKE2b-256 checksum
How to use checksums
034c4f10a18c50f0b7772b5502be0b09d187efda081d9b83d660b8f2a77d25c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0.post20200511 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

Release files / wait4it-0.2.1-py3-none-any.whl

Download URL wait4it-0.2.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
515567dbf81e62af4579078824a7da32cf9b549bebb546052830d45fe963038c
BLAKE2b-256 checksum
How to use checksums
00f7bf19111f350693d250b6478da5d4e1a00edef008402395a9fc7fd7dd26fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0.post20200511 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page