-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added optional context param for tasks (#1523)
* added optional context param for tasks * checks for 3.11 or lower * test fixes * lint & skipping tests * only one check needed * lint + comments * better tests * removed comment --------- Co-authored-by: Victoria Hall <victoria.hall@microsoft.com>
- Loading branch information
1 parent
bbc683e
commit f4c9c2d
Showing
8 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/unittests/http_functions/create_task_with_context/function.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"scriptFile": "main.py", | ||
"bindings": [ | ||
{ | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "$return" | ||
} | ||
] | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/unittests/http_functions/create_task_with_context/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import asyncio | ||
import contextvars | ||
|
||
import azure.functions | ||
|
||
num = contextvars.ContextVar('num') | ||
|
||
|
||
async def count(name: str): | ||
# The number of times the loop is executed | ||
# depends on the val set in context | ||
val = num.get() | ||
for i in range(val): | ||
await asyncio.sleep(0.5) | ||
return f"Finished {name} in {val}" | ||
|
||
|
||
async def main(req: azure.functions.HttpRequest): | ||
# Create first task with context num = 5 | ||
num.set(5) | ||
first_ctx = contextvars.copy_context() | ||
first_count_task = asyncio.create_task(count("Hello World"), context=first_ctx) | ||
|
||
# Create second task with context num = 10 | ||
num.set(10) | ||
second_ctx = contextvars.copy_context() | ||
second_count_task = asyncio.create_task(count("Hello World"), context=second_ctx) | ||
|
||
# Execute tasks | ||
first_count_val = await first_count_task | ||
second_count_val = await second_count_task | ||
|
||
return f'{first_count_val + " | " + second_count_val}' |
15 changes: 15 additions & 0 deletions
15
tests/unittests/http_functions/create_task_without_context/function.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"scriptFile": "main.py", | ||
"bindings": [ | ||
{ | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "$return" | ||
} | ||
] | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/unittests/http_functions/create_task_without_context/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import asyncio | ||
|
||
import azure.functions | ||
|
||
|
||
async def count(name: str, num: int): | ||
# The number of times the loop executes is decided by a | ||
# user-defined param | ||
for i in range(num): | ||
await asyncio.sleep(0.5) | ||
return f"Finished {name} in {num}" | ||
|
||
|
||
async def main(req: azure.functions.HttpRequest): | ||
# No context is being sent into asyncio.create_task | ||
count_task = asyncio.create_task(count("Hello World", 5)) | ||
count_val = await count_task | ||
return f'{count_val}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters