Skip to content

Commit cc2b3e6

Browse files
authored
Merge pull request #219718 from davidmrdavid/dajusto/add-df-pystein-docs
Add DF docs for PyStein
2 parents f0f3880 + d9d3c64 commit cc2b3e6

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

articles/azure-functions/functions-bindings-triggers-python.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,126 @@ def test_function(mytimer: func.TimerRequest) -> None:
284284
logging.info('The timer is past due!')
285285
logging.info('Python timer trigger function ran at %s', utc_timestamp)
286286
```
287+
288+
## Durable Functions
289+
290+
Durable Functions also provides preview support of the V2 programming model. To try it out, install the Durable Functions SDK (PyPI package `azure-functions-durable`) from version `1.2.2` or greater. You can reach us in the [Durable Functions SDK for Python repo](https://github.com/Azure/azure-functions-durable-python) with feedback and suggestions.
291+
292+
293+
> [!NOTE]
294+
> Using [Extension Bundles](/azure-functions/functions-bindings-register#extension-bundles) is not currently supported when trying out the Python V2 programming model with Durable Functions, so you will need to manage your extensions manually.
295+
> To do this, remove the `extensionBundles` section of your `host.json` as described [here](/azure-functions/functions-bindings-register#extension-bundles) and run `func extensions install --package Microsoft.Azure.WebJobs.Extensions.DurableTask --version 2.9.1` on your terminal. This will install the Durable Functions extension for your app and will allow you to try out the new experience.
296+
297+
The Durable Functions Triggers and Bindings may be accessed from an instance `DFApp`, a subclass of `FunctionApp` that additionally exports Durable Functions-specific decorators.
298+
299+
Below is a simple Durable Functions app that declares a simple sequential orchestrator, all in one file!
300+
301+
```python
302+
import azure.functions as func
303+
import azure.durable_functions as df
304+
305+
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
306+
307+
# An HTTP-Triggered Function with a Durable Functions Client binding
308+
@myApp.route(route="orchestrators/{functionName}")
309+
@myApp.durable_client_input(client_name="client")
310+
async def durable_trigger(req: func.HttpRequest, client):
311+
function_name = req.route_params.get('functionName')
312+
instance_id = await client.start_new(function_name)
313+
response = client.create_check_status_response(req, instance_id)
314+
return response
315+
316+
# Orchestrator
317+
@myApp.orchestration_trigger(context_name="context")
318+
def my_orchestrator(context):
319+
result1 = yield context.call_activity("hello", "Seattle")
320+
result2 = yield context.call_activity("hello", "Tokyo")
321+
result3 = yield context.call_activity("hello", "London")
322+
323+
return [result1, result2, result3]
324+
325+
# Activity
326+
@myApp.activity_trigger(input_name="myInput")
327+
def hello(myInput: str):
328+
return "Hello " + myInput
329+
```
330+
331+
> [!NOTE]
332+
> Previously, Durable Functions orchestrators needed an extra line of boilerplate, usually at the end of the file, to be indexed:
333+
> `main = df.Orchestrator.create(<name_of_orchestrator_function>)`.
334+
> This is no longer needed in V2 of the Python programming model. This applies to Entities as well, which required a similar boilerplate through
335+
> `main = df.Entity.create(<name_of_entity_function>)`.
336+
337+
For reference, all Durable Functions Triggers and Bindings are listed below:
338+
339+
### Orchestration Trigger
340+
341+
```python
342+
import azure.functions as func
343+
import azure.durable_functions as df
344+
345+
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
346+
347+
@myApp.orchestration_trigger(context_name="context")
348+
def my_orchestrator(context):
349+
result = yield context.call_activity("Hello", "Tokyo")
350+
return result
351+
```
352+
353+
### Activity Trigger
354+
355+
```python
356+
import azure.functions as func
357+
import azure.durable_functions as df
358+
359+
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
360+
361+
@myApp.activity_trigger(input_name="myInput")
362+
def my_activity(myInput: str):
363+
return "Hello " + myInput
364+
```
365+
366+
### DF Client Binding
367+
368+
```python
369+
import azure.functions as func
370+
import azure.durable_functions as df
371+
372+
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
373+
374+
@myApp.route(route="orchestrators/{functionName}")
375+
@myApp.durable_client_input(client_name="client")
376+
async def durable_trigger(req: func.HttpRequest, client):
377+
function_name = req.route_params.get('functionName')
378+
instance_id = await client.start_new(function_name)
379+
response = client.create_check_status_response(req, instance_id)
380+
return response
381+
```
382+
383+
### Entity Trigger
384+
385+
```python
386+
import azure.functions as func
387+
import azure.durable_functions as df
388+
389+
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
390+
391+
@myApp.entity_trigger(context_name="context")
392+
def entity_function(context):
393+
current_value = context.get_state(lambda: 0)
394+
operation = context.operation_name
395+
if operation == "add":
396+
amount = context.get_input()
397+
current_value += amount
398+
elif operation == "reset":
399+
current_value = 0
400+
elif operation == "get":
401+
pass
402+
403+
context.set_state(current_value)
404+
context.set_result(current_value)
405+
```
406+
287407
## Next steps
288408

289409
+ [Python developer guide](./functions-reference-python.md)

0 commit comments

Comments
 (0)