Base
Batch processing utilities
Usage Documentation
CLASS | DESCRIPTION |
---|---|
AsyncBatchProcessor |
Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB asynchronously. |
BasePartialBatchProcessor |
|
BasePartialProcessor |
Abstract class for batch processors. |
BatchProcessor |
Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB. |
AsyncBatchProcessor ¶
AsyncBatchProcessor(
event_type: EventType,
model: BatchTypeModels | None = None,
raise_on_entire_batch_failure: bool = True,
)
Bases: BasePartialBatchProcessor
Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB asynchronously.
Example
Process batch triggered by SQS¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Process batch triggered by Kinesis Data Streams¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Process batch triggered by DynamoDB Data Streams¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
RAISES | DESCRIPTION |
---|---|
BatchProcessingError
|
When all batch records fail processing and raise_on_entire_batch_failure is True |
Limitations
- Sync record handler not supported, use BatchProcessor instead.
Source code in aws_lambda_powertools/utilities/batch/base.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
BasePartialBatchProcessor ¶
BasePartialBatchProcessor(
event_type: EventType,
model: BatchTypeModels | None = None,
raise_on_entire_batch_failure: bool = True,
)
Bases: BasePartialProcessor
PARAMETER | DESCRIPTION |
---|---|
event_type
|
Whether this is a SQS, DynamoDB Streams, or Kinesis Data Stream event
TYPE:
|
model
|
Parser's data model using either SqsRecordModel, DynamoDBStreamRecordModel, KinesisDataStreamRecord
TYPE:
|
raise_on_entire_batch_failure
|
Raise an exception when the entire batch has failed processing. When set to False, partial failures are reported in the response
TYPE:
|
Exceptions
BatchProcessingError Raised when the entire batch has failed processing
METHOD | DESCRIPTION |
---|---|
response |
Batch items that failed processing, if any |
Source code in aws_lambda_powertools/utilities/batch/base.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
response ¶
response() -> PartialItemFailureResponse
Batch items that failed processing, if any
Source code in aws_lambda_powertools/utilities/batch/base.py
274 275 276 |
|
BasePartialProcessor ¶
BasePartialProcessor()
Bases: ABC
Abstract class for batch processors.
METHOD | DESCRIPTION |
---|---|
async_process |
Async call instance's handler for each record. |
failure_handler |
Keeps track of batch records that failed processing |
process |
Call instance's handler for each record. |
success_handler |
Keeps track of batch records that were processed successfully |
Source code in aws_lambda_powertools/utilities/batch/base.py
65 66 67 68 |
|
async_process ¶
async_process() -> list[tuple]
Async call instance's handler for each record.
Note
We keep the outer function synchronous to prevent making Lambda handler async, so to not impact customers' existing middlewares. Instead, we create an async closure to handle asynchrony.
We also handle edge cases like Lambda container thaw by getting an existing or creating an event loop.
See: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html#runtimes-lifecycle-shutdown
Source code in aws_lambda_powertools/utilities/batch/base.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
failure_handler ¶
failure_handler(
record, exception: ExceptionInfo
) -> FailureResponse
Keeps track of batch records that failed processing
PARAMETER | DESCRIPTION |
---|---|
record
|
record that failed processing
|
exception
|
Exception information containing type, value, and traceback (sys.exc_info())
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
FailureResponse
|
"fail", exceptions args, original record |
Source code in aws_lambda_powertools/utilities/batch/base.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
process ¶
process() -> list[tuple]
Call instance's handler for each record.
Source code in aws_lambda_powertools/utilities/batch/base.py
91 92 93 94 95 |
|
success_handler ¶
success_handler(record, result: Any) -> SuccessResponse
Keeps track of batch records that were processed successfully
PARAMETER | DESCRIPTION |
---|---|
record
|
record that succeeded processing
|
result
|
result from record handler
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SuccessResponse
|
"success", result, original record |
Source code in aws_lambda_powertools/utilities/batch/base.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
BatchProcessor ¶
BatchProcessor(
event_type: EventType,
model: BatchTypeModels | None = None,
raise_on_entire_batch_failure: bool = True,
)
Bases: BasePartialBatchProcessor
Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB.
Example
Process batch triggered by SQS¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Process batch triggered by Kinesis Data Streams¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Process batch triggered by DynamoDB Data Streams¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
RAISES | DESCRIPTION |
---|---|
BatchProcessingError
|
When all batch records fail processing and raise_on_entire_batch_failure is True |
Limitations
- Async record handler not supported, use AsyncBatchProcessor instead.
Source code in aws_lambda_powertools/utilities/batch/base.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|