|
| 1 | +--- |
| 2 | +title: Continuous profiling for Python |
| 3 | +sidebarTitle: "Python continuous profiler" |
| 4 | +description: Configure and use the Python continuous profiler. |
| 5 | +weight: 30 |
| 6 | +--- |
| 7 | + |
| 8 | +{{< vendor/name >}} [Continuous Profiler](./cont-prof.md) is powered by [Blackfire](../../../increase-observability/application-metrics/blackfire.md). |
| 9 | +It is available directly from the [Console](/administration/web/_index.md), under the **Profiling** tab of your environments. |
| 10 | + |
| 11 | +The PHP continuous profiling is currently made accross 4 dimensions: |
| 12 | +- **CPU Time**: Time spent running on the CPU |
| 13 | +- **Wall-time**: Elapsed time per function call |
| 14 | +- **Heap Live Size**: Number of bytes allocated that are not yet garbage collected |
| 15 | +- **Allocated Memory**: Number of bytes allocated in memory |
| 16 | +- **Allocations**: Time spent running on the CPU |
| 17 | + |
| 18 | +The default sampling frequency is 100 Hz. This means the Python continuous profiler is |
| 19 | +collecting information 100 times per second. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +{{< vendor/name >}} Continuous Profiler requires [`Python >=3.7.0`](/languages/python/_index.md). |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +The [Blackfire Continuous Profiler Python library](https://github.com/blackfireio/python-continuous-profiling) is included by default in all |
| 28 | +Python images matching its requirements. There is no installation required. |
| 29 | + |
| 30 | +## Python continuous profiler API |
| 31 | + |
| 32 | +The Python profiler API (`profiler`) can be initiated with the following options: |
| 33 | + |
| 34 | +- `application_name`: the application name. |
| 35 | +- `period`: specifies the length at which to collect CPU profiles. The default is 45 seconds. |
| 36 | +- `upload_timeout`: observability data upload timeout. The default is 10 seconds. |
| 37 | +- `labels`: a dict containing the custom labels specific to the profile payload that is sent. |
| 38 | + |
| 39 | +The Python continuous profiler API has two functions: |
| 40 | + |
| 41 | +``` python |
| 42 | +def start(): |
| 43 | +def stop(): |
| 44 | +``` |
| 45 | + |
| 46 | +| Function | Description | |
| 47 | +| ---------------------- | ----------- | |
| 48 | +| `def start():` | The `start` function starts the continuous profiler probe. </br>It collects profiling information in the background and periodically uploads it to the Blackfire Agent until the ``stop`` function is called. | |
| 49 | +| `def stop():` |Stops the continuous profiling probe. | |
| 50 | + |
| 51 | +## Example |
| 52 | + |
| 53 | +Here is an example of how you can initiate the Python `profiler` on a basic app: |
| 54 | + |
| 55 | +1. Create `example.py` with the following code: |
| 56 | + |
| 57 | + ``` python |
| 58 | + def foo(): |
| 59 | + import time |
| 60 | + time.sleep(1.0) |
| 61 | + |
| 62 | + profiler = Profiler(application_name="my-python-app", labels={'my-extra-label': 'data'}) |
| 63 | + profiler.start() |
| 64 | + foo() |
| 65 | + profiler.stop() |
| 66 | + ``` |
| 67 | + |
| 68 | +2. Run the app: |
| 69 | + |
| 70 | + ``` bash |
| 71 | + python example.py |
| 72 | + ``` |
0 commit comments