> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Continuous profiling for Python

> Configure and use the Python continuous profiler.

> [!NOTE]
>
> By default, Upsun offers 15 minutes of continuous profiling per project and for free.
> To get 30 days of continuous profiling per project and for a fixed fee, [upgrade to the Continuous Profiling add-on](https://developer.upsun.com/docs/administration/billing/add-on-subscription#continuous-profiling-add-on).

Upsun Cloud [Continuous Profiler](https://developer.upsun.com/docs/observability/application-metrics/cont-prof.md) is powered by [Upsun Blackfire](https://developer.upsun.com/docs/observability/application-metrics/blackfire.md).
It is available directly in the [Console](https://developer.upsun.com/docs/administration/web), under **Apps and Services → your app → Profiling**.

The PHP continuous profiling is currently made across 4 dimensions:

* **CPU Time**:  Time spent running on the CPU
* **Wall-time**: Elapsed time per function call
* **Heap Live Size**: Number of bytes allocated that are not yet garbage collected
* **Allocated Memory**: Number of bytes allocated in memory
* **Allocations**: Time spent running on the CPU

The default sampling frequency is 100 Hz. This means the Python continuous profiler is
collecting information 100 times per second.

## Prerequisites

Upsun Cloud Continuous Profiler requires [`Python >=3.7.0`](https://developer.upsun.com/docs/languages/python).

## Installation

The [Upsun Blackfire Continuous Profiler Python library](https://github.com/blackfireio/python-continuous-profiling) is included by default in all
Python images matching its requirements. There is no installation required.

## Python continuous profiler API

The Python profiler API (`profiler`) can be initiated with the following options:

* `application_name`: the application name.
* `period`: specifies the length at which to collect CPU profiles. The default is 45 seconds.
* `upload_timeout`: observability data upload timeout. The default is 10 seconds.
* `labels`: a dict containing the custom labels specific to the profile payload that is sent.

The Python continuous profiler API has two functions:

```python theme={null}
def start():
def stop():
```

| Function | Description |
| - | - |
| `def start():` | The `start` function starts the continuous profiler probe. It collects profiling information in the background and periodically uploads it to the Upsun Blackfire Agent until the `stop` function is called. |
| `def stop():` | Stops the continuous profiling probe. |

## Example

Here is an example of how you can initiate the Python `profiler` on a basic app:

1. Create `example.py` with the following code:

   ```python theme={null}
   def foo():
     import time
     time.sleep(1.0)

   profiler = Profiler(application_name="my-python-app", labels={'my-extra-label': 'data'})
   profiler.start()
   foo()
   profiler.stop()
   ```

2. Run the app:

   ```bash theme={null}
   python example.py
   ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.