Reputation: 21
I have a lambda in C# with a simple foreach loop. The foreach loop invokes a python lambda for each item in the List..
foreach (NOAAQInfo noaaqInfo in batchItemsForMyPython)
{
Console.WriteLine($"invoking MyPython api for: {noaaqInfo.Email}");
Task<NOAAQInfo> t = SendToMyPython.executeLambda(dbLogic, noaaqInfo, context);
Task tCleanup = t.ContinueWith((antecedent) =>
{
Console.WriteLine($"MyPython success: {antecedent.Result.Email}");
});
}
Here is the function that actually invokes the Python Lambda.
internal static async Task<NOAAQInfo> executeLambda(DBLogic dbLogic, NOAAQInfo noaaqInfo, ILambdaContext context)
{
AmazonLambdaClient amazonLambdaClient = new AmazonLambdaClient();
NOAAQInfo noaaqInfoResult = null;
InvokeRequest request = new InvokeRequest()
{
FunctionName = MyPython,
Payload = JsonConvert.SerializeObject(payload)
};
CancellationToken cancellationToken = new CancellationToken();
string messageStr = null;
InvokeResponse invokeResponse = null;
invokeResponse = await amazonLambdaClient.InvokeAsync(request, cancellationToken);
if (invokeResponse.StatusCode == (int)HttpStatusCode.OK)
{
if (resp.body != VerifiedStrResponse)
{
noaaqInfoResult = noaaqInfo;
}
}
return noaaqInfoResult;
}
If i look at the logs for the C# lambda, i can see the 2 items being sent to the python lambda:
2021-02-19T14:04:39.837-08:00 invoking MyPython api for: [email protected]
2021-02-19T14:04:40.057-08:00 invoking MyPython api for: [email protected]
However, when I look at the Python lambda logs, i only see one item being received:
[INFO] 2021-02-19T14:04:40.837-08:00Z adafecd6-8f32-4b7f-8d46-94465ac03a63 received params:{'Email': '[email protected]'}
Why don't I see testBBB in the python logs? ie there should be 2 invocations of the python lambda.
I also double checked that there wasn't an additional log stream, and that the python lambda doesnt have any concurrency limit set.
Really scratching my head on this one, as I'm not getting any errors. It's just that only one item is getting sent to the python lambda.
Additionally, The logging line inside the ContinueWith() isn't showing either.
Upvotes: 0
Views: 323
Reputation: 21487
You are missing some awaits.
I believe this should work for you:
var noaaqInfos = batchItemsForMyPython.Select(async noaaqInfo => {
Console.WriteLine($"invoking MyPython api for: {noaaqInfo.Email}");
var t = await SendToMyPython.executeLambda(dbLogic, noaaqInfo, context);
Console.WriteLine($"MyPython success: {t.Result.Email}");
return t;
});
Task.WhenAll(noaaqInfos);
Also much easier if you move the Console stuff to your async method, remove DbLogic and ILambdaContext that you don't seem to be using, then the call just becomes:
var tasks = batchItemsForPython.Select(SendToMyPython.executeLambda);
Task.WhenAll(tasks);
Upvotes: 1