You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Memory profiling on Python apps in Azure Functions
3
-
description: Learn how to profile Python apps memory usage and identify memory bottleneck.
2
+
title: Memory profiling of Python apps in Azure Functions
3
+
description: Learn how to profile the memory usage of Python apps and identify memory bottleneck.
4
4
ms.topic: how-to
5
-
ms.date: 3/22/2021
5
+
ms.date: 4/11/2023
6
6
ms.devlang: python
7
7
ms.custom: devx-track-python, py-fresh-zinc
8
8
---
9
9
# Profile Python apps memory usage in Azure Functions
10
10
11
-
During development or after deploying your local Python function app project to Azure, it's a good practice to analyze for potential memory bottlenecks in your functions. Such bottlenecks can decrease the performance of your functions and lead to errors. The following instruction show you how to use the [memory-profiler](https://pypi.org/project/memory-profiler) Python package, which provides line-by-line memory consumption analysis of your functions as they execute.
11
+
During development or after deploying your local Python function app project to Azure, it's a good practice to analyze for potential memory bottlenecks in your functions. Such bottlenecks can decrease the performance of your functions and lead to errors. The following instructions show you how to use the [memory-profiler](https://pypi.org/project/memory-profiler) Python package, which provides line-by-line memory consumption analysis of your functions as they execute.
12
12
13
13
> [!NOTE]
14
-
> Memory profiling is intended only for memory footprint analysis on development environment. Please do not apply the memory profiler on production function apps.
14
+
> Memory profiling is intended only for memory footprint analysis in development environments. Please do not apply the memory profiler on production function apps.
15
15
16
16
## Prerequisites
17
17
18
18
Before you start developing a Python function app, you must meet these requirements:
19
19
20
-
*[Python 3.6.x or above](https://www.python.org/downloads/release/python-374/). To check the full list of supported Python versions in Azure Functions, please visit[Python developer guide](functions-reference-python.md#python-version).
20
+
*[Python 3.7 or above](https://www.python.org/downloads). To check the full list of supported Python versions in Azure Functions, see the[Python developer guide](functions-reference-python.md#python-version).
21
21
22
-
* The [Azure Functions Core Tools](functions-run-local.md#v2) version 3.x.
22
+
* The [Azure Functions Core Tools](functions-run-local.md#v2), version 4.x or greater. Check your version with `func --version`. To learn about updating, see [Azure Functions Core Tools on GitHub](https://github.com/Azure/azure-functions-core-tools).
23
23
24
24
*[Visual Studio Code](https://code.visualstudio.com/) installed on one of the [supported platforms](https://code.visualstudio.com/docs/supporting/requirements#_platforms).
25
25
@@ -29,9 +29,9 @@ Before you start developing a Python function app, you must meet these requireme
29
29
30
30
## Memory profiling process
31
31
32
-
1. In your requirements.txt, add `memory-profiler` to ensure the package will be bundled with your deployment. If you are developing on your local machine, you may want to [activate a Python virtual environment](create-first-function-cli-python.md#create-venv) and do a package resolution by `pip install -r requirements.txt`.
32
+
1. In your requirements.txt, add `memory-profiler` to ensure the package is bundled with your deployment. If you're developing on your local machine, you may want to [activate a Python virtual environment](create-first-function-cli-python.md#create-venv) and do a package resolution by `pip install -r requirements.txt`.
33
33
34
-
2. In your function script (usually \_\_init\_\_.py), add the following lines above the `main()` function. This will ensure the root logger reports the child logger names, so that the memory profiling logs are distinguishable by the prefix `memory_profiler_logs`.
34
+
2. In your function script (for example, *\_\_init\_\_.py* for the Python v1 programming model and *function_app.py* for the v2 model), add the following lines above the `main()` function. These lines ensure the root logger reports the child logger names, so that the memory profiling logs are distinguishable by the prefix `memory_profiler_logs`.
35
35
36
36
```python
37
37
import logging
@@ -40,51 +40,64 @@ Before you start developing a Python function app, you must meet these requireme
3. Apply the following decorator above any functions that need memory profiling. This does not work directly on the trigger entrypoint `main()` method. You need to create subfunctions and decorate them. Also, due to a memory-profiler known issue, when applying to an async coroutine, the coroutine return value will always be None.
43
+
3. Apply the following decorator above any functions that need memory profiling. The decorator doesn't work directly on the trigger entrypoint `main()` method. You need to create subfunctions and decorate them. Also, due to a memory-profiler known issue, when applying to an async coroutine, the coroutine return value is always `None`.
4. Test the memory profiler on your local machine by using azure Functions Core Tools command `func host start`. This should generate a memory usage reportwithfile name, line of code, memory usage, memory increment, and the line content in it.
48
+
4. Test the memory profiler on your local machine by using Azure Functions Core Tools command `func host start`. When you invoke the functions, they should generate a memory usage report. The report containsfile name, line of code, memory usage, memory increment, and the line content in it.
49
49
50
-
5. To check the memory profiling logs on an existing function app instance in Azure, you can query the memory profiling logs in recent invocations by pasting the following Kusto queries in Application Insights, Logs.
50
+
5. To check the memory profiling logs on an existing function app instance in Azure, you can query the memory profiling logs for recent invocations with [Kusto](/azure/azure-monitor/logs/log-query-overview) queries in Application Insights, Logs.
51
51
52
-
:::image type="content" source="media/python-memory-profiler-reference/application-insights-query.png" alt-text="Query memory usage of a Python app in Application Insights":::
52
+
:::image type="content" source="media/python-memory-profiler-reference/application-insights-query.png" alt-text="Screenshot showing the query memory usage of a Python app in Application Insights.":::
53
53
54
-
```text
55
-
traces
56
-
| where timestamp > ago(1d)
57
-
| where message startswith_cs "memory_profiler_logs:"
Here is an example of performing memory profiling on an asynchronous and a synchronous HTTP triggers, named "HttpTriggerAsync" and "HttpTriggerSync" respectively. We will build a Python function app that simply sends out GET requests to the Microsoft's home page.
72
+
Here's an example of performing memory profiling on an asynchronous and a synchronous HTTP trigger, named "HttpTriggerAsync" and "HttpTriggerSync" respectively. We'll build a Python function app that simply sends out GET requests to the Microsoft's home page.
73
73
74
74
### Create a Python function app
75
75
76
76
A Python function app should follow Azure Functions specified [folder structure](functions-reference-python.md#folder-structure). To scaffold the project, we recommend using the Azure Functions Core Tools by running the following commands:
77
77
78
+
# [v1](#tab/v1)
79
+
78
80
```bash
79
81
func init PythonMemoryProfilingDemo --python
80
82
cd PythonMemoryProfilingDemo
81
83
func new -l python -t HttpTrigger -n HttpTriggerAsync -a anonymous
82
84
func new -l python -t HttpTrigger -n HttpTriggerSync -a anonymous
For the Python V2 programming model, triggers and bindings are created as decorators within the Python file itself, the *function_app.py*file. For information on how to create a new function with the new programming model, see the [Azure Functions Python developer guide](https://aka.ms/pythonprogrammingmodel). `func new` isn't supported for the preview of the V2 Python programming model.
95
+
96
+
---
97
+
85
98
### Update file contents
86
99
87
-
The requirements.txt defines the packages that will be used in our project. Besides the Azure Functions SDK and memory-profiler, we introduce `aiohttp` for asynchronous HTTP requests and `requests` for synchronous HTTP calls.
100
+
The *requirements.txt* defines the packages that are used in our project. Besides the Azure Functions SDKand memory-profiler, we introduce `aiohttp`for asynchronous HTTP requests and`requests`for synchronous HTTP calls.
88
101
89
102
```text
90
103
# requirements.txt
@@ -95,7 +108,11 @@ aiohttp
95
108
requests
96
109
```
97
110
98
-
We also need to rewrite the asynchronous HTTP trigger `HttpTriggerAsync/__init__.py` and configure the memory profiler, root logger format, and logger streaming binding.
111
+
Create the asynchronous HTTP trigger.
112
+
113
+
# [v1](#tab/v1)
114
+
115
+
Replace the code in the asynchronous HTTP trigger *HttpTriggerAsync/\_\_init\_\_.py* with the following code, which configures the memory profiler, root logger format, and logger streaming binding.
For synchronous HTTP trigger, please refer to the following `HttpTriggerSync/__init__.py` code section:
148
+
# [v2](#tab/v2)
149
+
150
+
Replace the code in the *function_app.py*filewith the following code, which configures the memory profiler, root logger format, and logger streaming binding.
151
+
152
+
```python
153
+
# function_app.py
154
+
import azure.functions as func
155
+
import logging
156
+
import aiohttp
157
+
import requests
158
+
import memory_profiler
159
+
160
+
app= func.FunctionApp()
161
+
162
+
# Update root logger's format to include the logger name. Ensure logs generated
163
+
# from memory profiler can be filtered by "memory_profiler_logs" prefix.
0 commit comments