Azure Functions 与 Python:触发器
Python 开发人员可以使用 Azure Functions 创建轻量级、可扩展且高效的无服务器应用程序。在这篇文章中,我们将重点关注触发器。
Azure Functions 中的触发器是什么?
触发器是 Azure Functions 的基础。它们决定如何调用函数。每个函数必须有一个触发器,并且触发器类型决定了该函数可用的数据负载。 Azure 支持各种触发器,包括:
1. HTTP 触发器
- 允许通过 HTTP 请求调用函数。
- 对于构建 API 或响应 Webhook 很有用。
- 示例:
import azure.functions as func import datetime import json import logging app = func.FunctionApp() @app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS) def http_trigger(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') return func.HttpResponse("Hello world from HTTP trigger")
参数:
-
route: 指定 HTTP 触发器将响应的 URL 路径。在这种情况下,可以在
/api/http_trigger. 访问该函数。
-
auth_level: 确定函数的身份验证级别。选项包括:
- 匿名:无需身份验证。
- 功能:需要特定功能的键。
- ADMIN:需要管理员级密钥。
2.定时器触发
- 根据时间表执行功能。
- Cron 表达式用于调度。
- 示例:
import azure.functions as func import datetime import json import logging app = func.FunctionApp() @app.timer_trigger(schedule="0 */5 * * * *", arg_name="myTimer", run_on_startup=False, use_monitor=False) 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.')
参数:
- schedule: 使用 CRON 表达式定义计划。这里,0 */5 * * * * 指定函数从第 0 秒开始每 5 分钟运行一次。
- arg_name: 传递给函数的参数名称,代表 TimerRequest 对象。
- run_on_startup: 如果设置为 True,该函数将在应用程序启动时立即执行。默认值为 False。
- use_monitor: 确定 Azure 是否应监视错过的计划执行。如果为 True,Azure 将确保重试错过的执行。默认为 True。在此示例中,它设置为 False。
3.斑点触发器
- 响应 Azure Blob 存储中的更改(例如文件上传)。
- 示例:
import azure.functions as func import datetime import json import logging app = func.FunctionApp() @app.blob_trigger(arg_name="myblob", path="blobname", connection="BlobStorageConnectionString") def BlobTrigger(myblob: func.InputStream): logging.info(f"Python blob trigger function processed blob" f"Name: {myblob.name}" f"Blob Size: {myblob.length} bytes")
参数:
- arg_name: 指定函数中表示 blob 数据的参数名称。这是我的blob。
- path: 函数侦听的 Blob 存储容器中的路径。在此示例中,它是 blobname。
- connection: 指包含 Blob 存储帐户的连接字符串的应用程序设置的名称。这是 BlobStorageConnectionString。
4.队列触发
- 由添加到 Azure 存储队列的消息触发。
- 示例:
import azure.functions as func import datetime import json import logging app = func.FunctionApp() @app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS) def http_trigger(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') return func.HttpResponse("Hello world from HTTP trigger")
参数:
- arg_name:指定函数中代表队列消息的参数名称。在这里,是 azqueue。
- queue_name:函数侦听的 Azure 存储队列的名称。在本例中,它是队列名称。
- 连接:指包含 Azure 存储队列连接字符串的应用程序设置。在这里,它是 QueueConnectionString。
5.事件中心触发器
- 由发送到 Azure 事件中心的事件触发。
- 示例:
import azure.functions as func import datetime import json import logging app = func.FunctionApp() @app.timer_trigger(schedule="0 */5 * * * *", arg_name="myTimer", run_on_startup=False, use_monitor=False) 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.')
参数:
- arg_name: 这指定将在函数中接收事件数据的参数的名称。在这种情况下,azeventhub将是代表传入EventHubEvent的变量。
- event_hub_name: 这表示函数正在侦听的事件中心的名称。将 eventhubname 替换为事件中心的实际名称。
- 连接: 这是指包含事件中心连接字符串的应用程序设置的名称。确保 Azure Function App 的设置包含名为 EventHubConnectionString 的条目以及相应的连接字符串值。
6.服务总线队列触发器
- 由添加到 Azure 服务总线队列的消息触发。
- 示例:
import azure.functions as func import datetime import json import logging app = func.FunctionApp() @app.blob_trigger(arg_name="myblob", path="blobname", connection="BlobStorageConnectionString") def BlobTrigger(myblob: func.InputStream): logging.info(f"Python blob trigger function processed blob" f"Name: {myblob.name}" f"Blob Size: {myblob.length} bytes")
参数:
- arg_name: 这指定将在函数中接收消息数据的参数的名称。在这种情况下,azservicebus 将是代表传入 ServiceBusMessage 的变量。
- queue_name: 这表示函数正在侦听的服务总线队列的名称。将 servicebusqueuename 替换为您的服务总线队列的实际名称。
- connection: 这是指包含服务总线连接字符串的应用程序设置的名称。确保 Azure Function App 的设置包含名为 ServiceBusConnectionString 的条目以及相应的连接字符串值。
7. ServiceBus 主题触发器
- 由发布到 Azure 服务总线主题的消息触发。
- 示例:
import azure.functions as func import datetime import json import logging app = func.FunctionApp() @app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS) def http_trigger(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') return func.HttpResponse("Hello world from HTTP trigger")
参数:
- arg_name:指定函数中代表服务总线消息的参数名称。在这里,它是 azservicebus。
- subscription_name:触发器侦听的服务总线订阅的名称。
- topic_name:触发器侦听的服务总线主题的名称。在此示例中,它是 servicebustopicname。
- 连接:指包含 Azure 服务总线命名空间的连接字符串的应用程序设置。这里是ServiceBusConnectionString。
其他触发因素
- Cosmos DB 触发器: 利用更改源机制响应 Azure Cosmos DB 数据库中的更改(插入和更新)。
- Dapr 发布输出绑定: 允许函数在执行期间将消息发布到 Dapr 主题,从而促进微服务之间的通信。
- Dapr 服务调用触发器: 允许其他支持 Dapr 的服务直接调用函数,支持服务到服务通信。
- Dapr 主题触发器: 执行函数以响应通过 Dapr 的发布-订阅消息传递模式发布到特定主题的消息。
- 事件网格触发器:当事件发送到 Azure 事件网格主题时激活函数,从而实现反应式事件驱动架构。
以上是Azure Functions 与 Python:触发器的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...
