我有一个时间触发的函数和另一个带有http触发器的函数,它们托管在同一个azure函数中。我想从时间触发函数向http触发函数发出http请求。另外,我不希望我的时间触发函数等待 http 触发函数完成。由于它是一个高处理耗时的任务,并且http触发的函数可以独立运行。我只是想知道如何处理这种情况。
我想提前返回请求并显示消息“函数正在运行”。我尝试使用异步编程,但没有成功,你能告诉我其他方式来处理它吗?
您可以使用asyncio来实现它。我使用了定时器触发和http触发函数的示例代码,并从定时器触发函数调用了http触发函数。
import azure.functions as func
import logging
import aiohttp
import asyncio
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="http_trigger")
async def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
await asyncio.sleep(70)
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
@app.timer_trigger(schedule="* * * * *", arg_name="myTimer", run_on_startup=False,
use_monitor=False)
async def timer_trigger(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function executed.')
# Define the URL for the HTTP-triggered function
url = "http://localhost:7071/api/http_trigger"
# Define the asynchronous request
async def trigger_http_function():
async with aiohttp.ClientSession() as client:
async with client.get(url) as response:
logging.info(f"Triggered HTTP function with status: {response.status}")
asyncio.create_task(trigger_http_function())
logging.info('HTTP request sent, not waiting for completion.')
我得到了预期的回应。定时器触发功能完成,无需等待http触发功能完成。
Azure Functions Core Tools
Core Tools Version: 4.0.5907 Commit hash: N/A +807e89766a92b14fd07b9f0bc2bea1d8777ab209 (64-bit)
Function Runtime Version: 4.834.3.22875
[2024-08-28T07:01:24.491Z] 0.02s - Debugger warning: It seems that frozen modules are being used, which may
[2024-08-28T07:01:24.494Z] 0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
[2024-08-28T07:01:24.495Z] 0.00s - to python to disable frozen modules.
[2024-08-28T07:01:24.496Z] 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
[2024-08-28T07:01:24.579Z] Worker process started and initialized.
Functions:
http_trigger: http://localhost:7071/api/http_trigger
timer_trigger: timerTrigger
For detailed output, run func with --verbose flag.
[2024-08-28T07:01:29.499Z] Host lock lease acquired by instance ID '0000000000000000000000000D2022A4'.
[2024-08-28T07:02:00.027Z] Executing 'Functions.timer_trigger' (Reason='Timer fired at 2024-08-28T12:32:00.0023600+05:30', Id=8bc317c7-1900-4c8a-8a41-bb1f704673ca)
[2024-08-28T07:02:00.081Z] HTTP request sent, not waiting for completion.
[2024-08-28T07:02:00.081Z] Python timer trigger function executed.
[2024-08-28T07:02:00.101Z] Executed 'Functions.timer_trigger' (Succeeded, Id=8bc317c7-1900-4c8a-8a41-bb1f704673ca, Duration=89ms)
[2024-08-28T07:02:00.412Z] Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=7478e2d9-ab52-4c4a-a9e4-74349167c196)
[2024-08-28T07:02:00.426Z] Python HTTP trigger function processed a request.
[2024-08-28T07:03:00.023Z] Executing 'Functions.timer_trigger' (Reason='Timer fired at 2024-08-28T12:33:00.0234478+05:30', Id=6f654043-89dc-4583-bdf1-e21a1b23e123)
[2024-08-28T07:03:00.029Z] Python timer trigger function executed.
[2024-08-28T07:03:00.029Z] HTTP request sent, not waiting for completion.
[2024-08-28T07:03:00.030Z] Executed 'Functions.timer_trigger' (Succeeded, Id=6f654043-89dc-4583-bdf1-e21a1b23e123, Duration=6ms)
[2024-08-28T07:03:00.292Z] Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=0051b829-9544-40de-85da-a45c1ab0e4a3)
[2024-08-28T07:03:00.297Z] Python HTTP trigger function processed a request.
[2024-08-28T07:03:10.442Z] Executed 'Functions.http_trigger' (Succeeded, Id=7478e2d9-ab52-4c4a-a9e4-74349167c196, Duration=70034ms)
传送门-