{"id":9199,"library":"py-healthcheck","title":"py-healthcheck: Flask and Tornado Healthcheck Endpoints","description":"py-healthcheck is a Python library that simplifies adding healthcheck and environment dump endpoints to Flask or Tornado web applications. It allows developers to define custom check functions to monitor application dependencies and internal state, exposing their status via a configurable HTTP route. The latest stable version is 1.10.1, though its release cadence appears irregular based on PyPI and GitHub history, with the most recent update in June 2022.","status":"active","version":"1.10.1","language":"en","source_language":"en","source_url":"https://github.com/ateliedocodigo/py-healthcheck","tags":["healthcheck","flask","tornado","monitoring","microservices","api"],"install":[{"cmd":"pip install py-healthcheck","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for Flask application integration.","package":"Flask","optional":true},{"reason":"Required for Tornado application integration.","package":"Tornado","optional":true}],"imports":[{"symbol":"HealthCheck","correct":"from healthcheck import HealthCheck"},{"symbol":"EnvironmentDump","correct":"from healthcheck import EnvironmentDump"},{"symbol":"TornadoHandler","correct":"from healthcheck import TornadoHandler"}],"quickstart":{"code":"from flask import Flask\nfrom healthcheck import HealthCheck, EnvironmentDump\nimport os\n\napp = Flask(__name__)\n\nhealth = HealthCheck(app, '/healthcheck')\nenvdump = EnvironmentDump(app, '/environment')\n\ndef redis_available():\n    # Simulate a Redis check\n    # In a real app, you'd connect to Redis here\n    if os.environ.get('MOCK_REDIS_FAILURE') == 'true':\n        return False, \"Redis connection failed (mocked)\"\n    return True, \"redis ok\"\n\ndef database_check():\n    # Simulate a database check\n    # In a real app, you'd check DB connection/query\n    if os.environ.get('MOCK_DB_FAILURE') == 'true':\n        return False, \"Database connection failed (mocked)\"\n    return True, \"database ok\"\n\nhealth.add_check(redis_available)\nhealth.add_check(database_check)\n\ndef application_data():\n    return {\n        \"maintainer\": \"Your Name\",\n        \"git_repo\": \"https://github.com/your/repo\",\n        \"version\": \"1.0.0\"\n    }\n\nenvdump.add_section(\"application\", application_data)\n\n# To run the Flask app (for demonstration, use 'flask run' in production)\nif __name__ == '__main__':\n    # Example usage: http://127.0.0.1:5000/healthcheck\n    # Example usage: http://127.0.0.1:5000/environment\n    app.run(debug=True)","lang":"python","description":"This Flask quickstart demonstrates setting up `/healthcheck` and `/environment` endpoints. Custom functions (`redis_available`, `database_check`) are added to the health check. The `EnvironmentDump` also includes custom application data. Run with `python your_app.py` or `flask run` and access `/healthcheck` and `/environment` in your browser. Set `MOCK_REDIS_FAILURE=true` or `MOCK_DB_FAILURE=true` environment variables to see failure states."},"warnings":[{"fix":"Initialize `HealthCheck(success_ttl=None, failed_ttl=None)` or tune `success_ttl` and `failed_ttl` parameters as needed.","message":"Health check results are cached by default (27 seconds for success, 9 seconds for failure). This can obscure immediate service degradation. To perform real-time checks, initialize `HealthCheck` with `success_ttl=None` and `failed_ttl=None`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure all functions passed to `health.add_check()` adhere to the `(bool, str)` return signature. For example: `return True, \"Service is up\"` or `return False, \"Service is down: error details\"`.","message":"All custom health checker functions must strictly return a tuple of `(bool, str)`. The boolean indicates success/failure, and the string provides a message. Exceptions within a checker function are caught and reported as failures.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review documentation for `HealthCheck` or `add_check` for specific timeout parameters. Alternatively, implement timeouts using Python's `concurrent.futures` or `asyncio.wait_for` within your custom checker functions. The release notes suggest this is an added capability.","message":"Starting with version 1.6.0, the library added support for timeouts on execution checkers. If your custom checks perform long-running operations, consider implementing explicit timeouts within your checker functions or exploring configuration options for the `HealthCheck` instance to prevent health checks from blocking.","severity":"gotcha","affected_versions":">=1.6.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install py-healthcheck` to install the library.","cause":"The `py-healthcheck` library has not been installed or is not accessible in the current Python environment.","error":"ModuleNotFoundError: No module named 'healthcheck'"},{"fix":"For Flask, ensure `HealthCheck(app, '/healthcheck')` and `EnvironmentDump(app, '/environment')` are correctly instantiated with your `app` object and desired paths. For Tornado, ensure `TornadoHandler` is correctly added to `app.add_handlers` or `tornado.web.Application` routes.","cause":"The health check or environment dump endpoints have not been correctly registered with your Flask or Tornado application.","error":"HTTP 404 Not Found when accessing /healthcheck or /environment"},{"fix":"Modify your checker function to return a tuple, e.g., `return True, \"All good\"`.","cause":"A custom health checker function returned only a boolean or another non-iterable type, instead of the required `(bool, str)` tuple.","error":"TypeError: 'bool' object is not iterable"}]}