Python Heartbeat - Code Examples
Monitor your Python scripts, Celery jobs, and notebooks with MoniTao.
Python is ubiquitous in data processing, machine learning, and system scripting. Your critical Python jobs deserve reliable monitoring.
Here's how to integrate MoniTao heartbeat with the most common Python HTTP libraries.
With the requests library
The most popular and simple method:
import requests\n\n# Your processing\nprocess_data()\n\n# Ping MoniTao\nrequests.post('https://monitao.com/api/heartbeat/ping/YOUR_TOKEN')
No dependency: urllib
If you don't want external dependencies:
from urllib import request\n\nreq = request.Request(\n 'https://monitao.com/api/heartbeat/ping/YOUR_TOKEN',\n method='POST'\n)\nrequest.urlopen(req)
seo.heartbeat_python.decorator_title
seo.heartbeat_python.decorator_intro
seo.heartbeat_python.decorator_example
FAQ - Python Heartbeat
How do I integrate with Celery?
Add the ping at the end of your task, or use Celery's task_success signal to automatically trigger the ping after each successful task.
My script can fail mid-execution. Will the ping be sent?
No, that's the intended behavior! Place the ping at the very end of the script. If the script crashes before, no ping = MoniTao alerts.
How do I send execution duration with the ping?
Measure time with time.time() and send it as a parameter: requests.post(url, json={"duration": elapsed_seconds})
Should I handle requests.post errors?
Recommended: try/except with timeout. If MoniTao is unreachable, log the error but don't block your script.
Ready to Sleep Soundly?
Start free, no credit card required.