@@ -54,7 +54,7 @@ def async_batch_processor(
54
54
>>> return payload
55
55
>>>
56
56
>>> @async_batch_processor(record_handler=async_record_handler, processor=processor)
57
- >>> async def lambda_handler(event, context: LambdaContext ):
57
+ >>> def lambda_handler(event, context):
58
58
>>> return processor.response()
59
59
60
60
Limitations
@@ -183,3 +183,67 @@ def handler(event, context):
183
183
processor .process ()
184
184
185
185
return processor .response ()
186
+
187
+
188
+ def async_process_partial_response (
189
+ event : Dict ,
190
+ record_handler : Callable ,
191
+ processor : AsyncBatchProcessor ,
192
+ context : LambdaContext | None = None ,
193
+ ) -> PartialItemFailureResponse :
194
+ """
195
+ Higher level function to handle batch event processing asynchronously.
196
+
197
+ Parameters
198
+ ----------
199
+ event: Dict
200
+ Lambda's original event
201
+ record_handler: Callable
202
+ Callable to process each record from the batch
203
+ processor: AsyncBatchProcessor
204
+ Batch Processor to handle partial failure cases
205
+ context: LambdaContext
206
+ Lambda's context, used to optionally inject in record handler
207
+
208
+ Returns
209
+ -------
210
+ result: PartialItemFailureResponse
211
+ Lambda Partial Batch Response
212
+
213
+ Examples
214
+ --------
215
+ **Processes Lambda's SQS event**
216
+
217
+ ```python
218
+ from aws_lambda_powertools.utilities.batch import AsyncBatchProcessor, EventType, process_partial_response
219
+ from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
220
+
221
+ processor = BatchProcessor(EventType.SQS)
222
+
223
+ async def record_handler(record: SQSRecord):
224
+ return record.body
225
+
226
+ def handler(event, context):
227
+ return async_process_partial_response(
228
+ event=event, record_handler=record_handler, processor=processor, context=context
229
+ )
230
+ ```
231
+
232
+ Limitations
233
+ -----------
234
+ * Sync batch processors. Use `process_partial_response` instead.
235
+ """
236
+ try :
237
+ records : List [Dict ] = event .get ("Records" , [])
238
+ except AttributeError :
239
+ event_types = ", " .join (list (EventType .__members__ ))
240
+ docs = "https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/batch/#processing-messages-from-sqs" # noqa: E501 # long-line
241
+ raise ValueError (
242
+ f"Invalid event format. Please ensure batch event is a valid { processor .event_type .value } event. \n "
243
+ f"See sample events in our documentation for either { event_types } : \n { docs } "
244
+ )
245
+
246
+ with processor (records , record_handler , context ):
247
+ processor .async_process ()
248
+
249
+ return processor .response ()
0 commit comments