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, logger: Logger | None = None)
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
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
BasePartialBatchProcessor ¶
BasePartialBatchProcessor(event_type: EventType, model: BatchTypeModels | None = None, raise_on_entire_batch_failure: bool = True, logger: Logger | None = None)
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:
|
logger
|
Optional Logger instance to output warnings with tracebacks for failed records.
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
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
response ¶
response() -> PartialItemFailureResponse
Batch items that failed processing, if any
Source code in aws_lambda_powertools/utilities/batch/base.py
306 307 308 | |
BasePartialProcessor ¶
BasePartialProcessor(logger: Logger | None = None)
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
76 77 78 79 80 | |
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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
process ¶
process() -> list[tuple]
Call instance's handler for each record.
Source code in aws_lambda_powertools/utilities/batch/base.py
103 104 105 106 107 | |
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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
BatchProcessor ¶
BatchProcessor(event_type: EventType, model: BatchTypeModels | None = None, raise_on_entire_batch_failure: bool = True, logger: Logger | None = None)
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
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |