Skip to main content

A Health Check API Library for Multiprocessing Python Apps

license

This library adds a health check REST API to your multiprocessing apps. You can add health check function calls to your functions and library will track the function calls. This library supports multiprocessing threads. You can fetch a single overall app status by fetching http://<ip>:<port>/healthcheck, a single overall app liveness by fetching http://<ip>:<port>/liveness.

Influenced by prometheus client mp exporter. Health check functions will write healthy and liveness results <pid>.json file located in directory defined by PY_HEALTH_MULTIPROC_DIR. If the directory doesn't exist, health checks won't work.

Please clear the directory content before running your app. REST API

Each health check class will be run every 10 seconds by default. You can change this value by setting PY_HEALTH_RUN_PERIOD.

Usage

You can register your functions with add_check() decorator. You can set a timeout for your functions with set_timeout() if you process needs to check in regularly.

import multiprocessing as mp
import time

import healthcheck_python as hp


class P1(mp.Process):
	def __init__(self, timeout=0):
		super().__init__()
		self._stop_bit = mp.Event()
		self.timeout = timeout

	def close(self) -> None:
		self._stop_bit.set()

	def healthcheck(self):
		return True, "Healthcheck is OK"

	def do_something(self):
		time.sleep(5)

	def run(self):
		hp.init_check(timeout=self.timeout)
		hp.add_check(self.healthcheck)

		hp.live()
		while not self._stop_bit.is_set():
			hp.healthy()
			self.do_something()


hp.start_http_server(port=8080)

p1 = P1(timeout=10)
p1.start()

p2 = P1()
p2.start()

time.sleep(30)

p1.close()
p2.close()

p1.join()
p2.join()

It can also be used with threading. You need to create separate Healthcheck instances and start instances in your threads.

import multiprocessing as mp
import threading
import time

import healthcheck_python as hp
from healthcheck_python import create_health_check

HEALTHCHECK_P1 = create_health_check("P1", timeout=5)
HEALTHCHECK_P2 = create_health_check("P2")


class P1(threading.Thread):
	def __init__(self, timeout=0):
		super().__init__()
		self._stop_bit = mp.Event()
		self.timeout = timeout

	def close(self) -> None:
		self._stop_bit.set()

	def healthcheck(self):
		return True, "Healthcheck is OK"

	def do_something(self):
		time.sleep(5)

	def run(self):
		HEALTHCHECK_P1.add_check(self.healthcheck)
		HEALTHCHECK_P1.start()

		HEALTHCHECK_P1.live()
		while not self._stop_bit.is_set():
			HEALTHCHECK_P1.healthy()
			self.do_something()


class P2(threading.Thread):
	def __init__(self, timeout=0):
		super().__init__()
		self._stop_bit = mp.Event()
		self.timeout = timeout

	def close(self) -> None:
		self._stop_bit.set()

	def healthcheck(self):
		return True, "Healthcheck is OK"

	def do_something(self):
		time.sleep(5)

	def run(self):
		HEALTHCHECK_P2.add_check(self.healthcheck)
		HEALTHCHECK_P2.start()

		HEALTHCHECK_P2.live()
		while not self._stop_bit.is_set():
			HEALTHCHECK_P2.healthy()
			self.do_something()


hp.init_check(timeout=0)
hp.start_http_server(port=8080)

p1 = P1(timeout=10)
p1.start()

p2 = P2()
p2.start()

time.sleep(30)

p1.close()
p2.close()

p1.join()
p2.join()
$ curl http://localhost:8080/healthcheck
{"hostname": "my_app", "status": "success", "timestamp": 1684406235.474363, "results": [[{"checker": "healthcheck", "output": "Healthcheck is OK", "passed": true, "timestamp": 1684406230.9507005, "response_time": 5e-06}, {"checker": "P1", "output": "", "passed": true, "timestamp": 1684406230.9507082, "response_time": 0}]]}
$ curl http://localhost:8080/liveness
{"hostname": "my_app", "liveness": true, "timestamp": 1684406208.784097}

Metadata

Release files for healthcheck-python 1.2.2

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

Source distribution (sdist)

Source distribution for healthcheck-python 1.2.2
File Size Uploaded
healthcheck_python-1.2.2.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for healthcheck-python 1.2.2
File Interpreter ABI Platform
healthcheck_python-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 23.7 kB

Release files / healthcheck_python-1.2.2.tar.gz

Download URL healthcheck_python-1.2.2.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3cb51fcbfd8cf2e8b7c01e69d5bf7ef3003ff8dcd056ef2f51b6ffc2c977a849
BLAKE2b-256 checksum
How to use checksums
b9ec46f7a3eea8257cdc5d40707568ef6df5735a7dd5f2444660f6b0f5628f9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.13 Linux/6.15.7-200.fc42.x86_64

Release files / healthcheck_python-1.2.2-py3-none-any.whl

Download URL healthcheck_python-1.2.2-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
164a6497122dd0f5bf4820c03133c67efe8942a0c2f8af8cb702adf75f403934
BLAKE2b-256 checksum
How to use checksums
6cfb2462d04811e6af2ce2210329017e5180f3eaa8b13e2a1503337c63dc9dc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.13 Linux/6.15.7-200.fc42.x86_64

Release history Release notifications | RSS feed

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.3.0

2 release files

0.1.2

2 release files

0.1.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