Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
调用持久性 Lambda 函数
可以使用与默认 Lambda 函数相同的方法来调用持久性 Lambda 函数,但针对长时间运行的执行有一些重要注意事项。本节介绍持久性函数的调用模式、执行管理和最佳实践。
同步调用限制
持久性 Lambda 函数的同步调用限制为 15 分钟,这与默认 Lambda 函数相同。如果您的持久性函数需要运行超过 15 分钟,则必须异步调用该函数。
何时使用同步调用:适用于那些在 15 分钟内即可完成的持久性函数,以及那些您需要即时结果的情况,例如快速审批流程或短时间的数据处理任务。
长时间运行的工作流程的异步调用
对于运行时间可能超过 15 分钟的持久性函数,请使用异步调用。这使得该函数能够在您的客户端接收到即时确认的同时继续运行。
- TypeScript
-
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
const client = new LambdaClient({});
// Asynchronous invocation
const command = new InvokeCommand({
FunctionName: "my-durable-function",
InvocationType: "Event", // Asynchronous
Payload: JSON.stringify({ orderId: "12345" })
});
await client.send(command);
- Python
-
import boto3
import json
client = boto3.client('lambda')
# Asynchronous invocation
response = client.invoke(
FunctionName='my-durable-function',
InvocationType='Event', # Asynchronous
Payload=json.dumps({'order_id': '12345'})
)
执行管理 API
Lambda 提供相关 API 来管理和监控持久性函数的执行情况,包括列出执行记录、获取执行状态以及停止正在运行的执行。
- TypeScript
-
// Get execution status
const statusCommand = new InvokeCommand({
FunctionName: "my-durable-function",
InvocationType: "RequestResponse",
Payload: JSON.stringify({
action: "getStatus",
executionId: "exec-123"
})
});
const result = await client.send(statusCommand);
- Python
-
# Get execution status
response = client.invoke(
FunctionName='my-durable-function',
InvocationType='RequestResponse',
Payload=json.dumps({
'action': 'get_status',
'execution_id': 'exec-123'
})
)