# Python

> Install pulse-python and add one line to Django, FastAPI, Flask, or a script. Pulse then monitors your dependencies, scheduled jobs, and logs.

- Section: Packages
- Availability: From the Free plan, logs on Pro and Team. Python 3.9 or later, with no required dependency.
- URL: https://pulse-app.si/en/docs/packages/python

## Install

**zsh**

```bash
pip install pulse-python
export PULSE_KEY=pk_live_…
```

The key must be in the process environment. Then add the package for your framework. The first report goes out 2 seconds after startup.

### Django

**settings.py**

```python
INSTALLED_APPS = [
    "django.contrib.admin",
    "pulse_python.django",
]
```

Pulse reads `DATABASES`, the Redis caches in `CACHES` and the Celery app. With `runserver`, only the child process starts the package. Short commands (`migrate`, `shell`, `test`, `collectstatic`…) don’t start it.

### FastAPI

**main.py**

```python
from pulse_python.fastapi import instrument

app = FastAPI(lifespan=lifespan)
instrument(app, watch=[engine, redis_client, celery_app])
```

`instrument` wraps the app’s `lifespan`. FastAPI has no registry to search, so pass your clients in `watch`. Async clients are recognized, and each check opens its own connection.

### Flask

**app.py**

```python
from pulse_python.flask import Pulse

app = Flask(__name__)
Pulse(app, watch=[redis_client, queue, scheduler])
```

Flask-SQLAlchemy is found in `app.extensions`. With an app factory, write `pulse = Pulse(watch=[…])`, then `pulse.init_app(app)`. With `flask run --debug`, only the reloaded process starts the package.

### Script

**worker.py**

```python
from pulse_python import start_pulse

start_pulse(watch=[engine, redis_client, queue])
```

`start_pulse` starts a `pulse-reporter` background thread and returns the reporter, or `None` without a key. There’s one per process, and a second call only adds what’s in `watch`. `pulse()` retrieves it and `stop_pulse()` stops it.

## Gunicorn, Uvicorn and Celery

Each process sends its report, and Pulse keeps the latest one received. With `gunicorn --preload`, Django and Flask start the package only in the master process, so the workers’ logs aren’t copied. With FastAPI, the `lifespan` runs in each worker.

A separate Celery worker that loads Django also starts the package, and it’s the one that sees whether jobs succeed or fail. Choose which one sends (`PULSE_DISABLED=true` in the other), or start the worker with `-B` and leave the report to it.

## What Pulse recognizes

| Object | What Pulse does | Name shown |
| --- | --- | --- |
| SQLAlchemy `Engine` and `AsyncEngine`, Flask-SQLAlchemy, Django connections | `SELECT 1` | PostgreSQL, MySQL, SQLite… |
| PyMongo `MongoClient` | `ping` | MongoDB |
| `redis.Redis`, `redis.asyncio.Redis`, Django Redis caches | `PING` | Redis |
| Celery app | Connects to the broker without publishing anything | `File celery`, `File emails`… |
| RQ `Queue` | Reads the number of jobs | `File emails`… |
| Celery `beat_schedule` | Schedule, next run, success | The job’s name |
| APScheduler scheduler | Schedule, last and next run | The job’s id |

Each check has 5 seconds to respond and only reads. A failing dependency, a job that crashed, or one more than 5 minutes past its scheduled run turns the app **Degraded**.

## Your own jobs and checks

The decorator works on a regular or async function. `track` wraps a single call. If it fails, the failure is recorded and the error comes back to you unchanged.

**jobs.py**

```python
from pulse_python import track, track_cron

@track_cron("renew-subscriptions")
def renew():
    subscriptions.renew()

track("cleanup", lambda: sessions.clean())
```

To check a route or anything else, add probes. `http_probe` makes a `GET` without following redirects and fails at 500 and above.

**main.py**

```python
from pulse_python import Probe, http_probe, start_pulse

start_pulse(probes=[
    http_probe("Route /health", "http://localhost:8000/health"),
    Probe(name="Storage", kind="custom", run=lambda: storage.ping()),
])
```

## Choose what is sent

The heartbeat is always sent. The rest is set by [variables](https://pulse-app.si/en/docs/packages/variables), or by options, which take priority over them: the same names in lowercase (`log_level`, `exclude`…), in `start_pulse`, `instrument`, `Pulse` or `PULSE` for Django. An unknown option produces a warning and is ignored.

**settings.py**

```python
PULSE = {
    "exclude": ["File emails"],
    "log_level": "WARN",
}
```

## Logs

On Pro and Team, the package adds a handler to the `logging` root logger, without touching yours. The logger’s name becomes the context, exception traces are kept, and secrets masked.

**stderr**

```text
ERROR:orbit.payments:Payment declined token=abc123
ERROR  orbit.payments  Payment declined token=***
```

Only messages that reach the root are seen. A logger with `propagate = False`, like Uvicorn’s access log, stays silent. The package writes its own messages under the `pulse_python` logger and doesn’t send them.

## If nothing arrives

| Symptom | What to do |
| --- | --- |
| `PULSE_KEY is missing` | The variable isn’t in the process environment: check the service, the container or the `.env` you load. |
| `Pulse key refused` | The key was replaced in the app: copy the new one, then restart. |
| `Pulse unreachable` | Network or firewall: your app keeps running and sending resumes automatically. |
| `the cryptography package is missing` | Run `pip install "pulse-python[secure]"`, or remove `PULSE_PUBLIC_KEY`. |
| A dependency doesn’t appear | With FastAPI, Flask or a script, pass it in `watch`. |

- [All variables](https://pulse-app.si/en/docs/packages/variables): Every setting and its default.
- [Exactly what is sent](https://pulse-app.si/en/docs/packages/payload): The precise format of reports and logs.
- [Live logs](https://pulse-app.si/en/docs/apps/logs): Read, filter, and follow them in the app.
