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
Copy file name to clipboardExpand all lines: articles/azure-functions/develop-python-worker-extensions.md
+24-10Lines changed: 24 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
title: Develop Python worker extensions for Azure Functions
3
3
description: Learn how to create and publish worker extensions that let you inject middleware behavior into Python functions running in Azure.
4
4
ms.topic: how-to
5
-
ms.date: 6/1/2021
5
+
ms.date: 04/13/2023
6
6
ms.custom: devx-track-python, py-fresh-zinc
7
7
---
8
8
9
9
# Develop Python worker extensions for Azure Functions
10
10
11
-
Azure Functions lets you integrate custom behaviors as part of Python function execution. This feature enables you to create business logic that customers can easily use in their own function apps. To learn more, see the [Python developer reference](functions-reference-python.md#python-worker-extensions).
11
+
Azure Functions lets you integrate custom behaviors as part of Python function execution. This feature enables you to create business logic that customers can easily use in their own function apps. To learn more, see the [Python developer reference](functions-reference-python.md#python-worker-extensions). Worker extensions are supported in both the v1 and v2 Python programming models.
12
12
13
13
In this tutorial, you'll learn how to:
14
14
> [!div class="checklist"]
@@ -20,9 +20,9 @@ In this tutorial, you'll learn how to:
20
20
21
21
Before you start, you must meet these requirements:
22
22
23
-
*[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, see the [Python developer guide](functions-reference-python.md#python-version).
23
+
*[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).
24
24
25
-
* The [Azure Functions Core Tools](functions-run-local.md#v2), version 3.0.3568 or later.
25
+
* The [Azure Functions Core Tools](functions-run-local.md#v2), version 4.0.5095 or later, which supports using the extension with the [v2 Python programming model](./functions-reference-python.md). Check your version with `func --version`.
26
26
27
27
*[Visual Studio Code](https://code.visualstudio.com/) installed on one of the [supported platforms](https://code.visualstudio.com/docs/supporting/requirements#_platforms).
28
28
@@ -48,7 +48,7 @@ The folder for your extension project should be like the following structure:
48
48
|**.venv/**| (Optional) Contains a Python virtual environment used for local development. |
49
49
|**python_worker_extension/**| Contains the source code of the Python worker extension. This folder contains the main Python module to be published into PyPI. |
50
50
|**setup.py**| Contains the metadata of the Python worker extension package. |
51
-
|**readme.md**|(Optional) Contains the instruction and usage of your extension. This content is displayed as the description in the home page in your PyPI project. |
51
+
|**readme.md**| Contains the instruction and usage of your extension. This content is displayed as the description in the home page in your PyPI project. |
52
52
53
53
### Configure project metadata
54
54
@@ -76,6 +76,19 @@ The `pre_invocation_app_level` method is called by the Python worker before the
76
76
77
77
Similarly, the `post_invocation_app_level` is called after function execution. This example calculates the elapsed time based on the start time and current time. It also overwrites the return value of the HTTP response.
78
78
79
+
### Create a readme.md
80
+
81
+
Create a readme.md file in the root of your extension project. This file contains the instructions and usage of your extension. The readme.md content is displayed as the description in the home page in your PyPI project.
82
+
83
+
```markdown
84
+
# Python Worker Extension Timer
85
+
86
+
In this file, tell your customers when they need to call `Extension.configure()`.
87
+
88
+
The readme should also document the extension capabilities, possible configuration,
89
+
and usage of your extension.
90
+
```
91
+
79
92
## Consume your extension locally
80
93
81
94
Now that you've created an extension, you can use it in an app project to verify it works as intended.
@@ -135,7 +148,8 @@ Now that you've created an extension, you can use it in an app project to verify
135
148
pip install -e <PYTHON_WORKER_EXTENSION_ROOT>
136
149
```
137
150
138
-
In this example, replace `<PYTHON_WORKER_EXTENSION_ROOT>` with the file location of your extension project.
151
+
In this example, replace `<PYTHON_WORKER_EXTENSION_ROOT>` with the root file location of your extension project.
152
+
139
153
When a customer uses your extension, they'll instead add your extension package location to the requirements.txt file, as in the following examples:
140
154
141
155
# [PyPI](#tab/pypi)
@@ -159,7 +173,7 @@ Now that you've created an extension, you can use it in an app project to verify
159
173
160
174
When running in Azure, you instead add `PYTHON_ENABLE_WORKER_EXTENSIONS=1` to the [app settings in the function app](functions-how-to-use-azure-function-app-settings.md#settings).
161
175
162
-
1. Add following two lines before the `main` function in \_\_init.py\_\_:
176
+
1. Add following two lines before the `main` function in *\_\_init.py\_\_* file for the v1 programming model, or in the *function_app.py* file for the v2 programming model:
163
177
164
178
```python
165
179
from python_worker_extension_timer import TimerExtension
@@ -174,9 +188,9 @@ Now that you've created an extension, you can use it in an app project to verify
174
188
175
189
1. In the browser, send a GET request to `https://localhost:7071/api/HttpTrigger`. You should see a response like the following, with the **TimeElapsed** data for the request appended.
176
190
177
-
<pre>
191
+
```
178
192
This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response. (TimeElapsed: 0.0009996891021728516 sec)
179
-
</pre>
193
+
```
180
194
181
195
## Publish your extension
182
196
@@ -211,7 +225,7 @@ To publish your extension to PyPI:
211
225
twine upload dist/*
212
226
```
213
227
214
-
You may need to provide your PyPI account credentials during upload.
228
+
You may need to provide your PyPI account credentials during upload. You can also test your package upload with `twine upload -r testpypi dist/*`. For more information, see the [Twine documentation](https://twine.readthedocs.io/en/stable/).
215
229
216
230
After these steps, customers can use your extension by including your package name in their requirements.txt.
0 commit comments