Skip to content

Instantly share code, notes, and snippets.

@LukeTillman
Created June 2, 2015 21:09
Show Gist options
  • Save LukeTillman/1320ee1527c7570a7fb2 to your computer and use it in GitHub Desktop.
Save LukeTillman/1320ee1527c7570a7fb2 to your computer and use it in GitHub Desktop.
Async Requests in Parallel
// Use a prepared statement for all the INSERTs
PreparedStatement prepared = session.Prepare("INSERT INTO sometable (column1, column2) VALUES (?, ?)");
// Create a list to hold in flight requests
var requestList = new List<Task>();
// Assume you have some collection of data that needs to be inserted
foreach(var data in dataToInsert)
{
// Provide data values to be inserted with our prepared statement for this piece of data
BoundStatement bound = prepared.Bind(data.Value1, data.Value2);
// Execute the statement async, saving the Task in our list of in-flight requests
requestList.Add(session.ExecuteAsync(bound));
// Wait for the in-flight requests to finish after 100 have been sent
if (requestList.Count == 100)
{
// This will throw an Exception if any of the requests failed with an Exception
await Task.WhenAll(requestList);
// Now that those 100 are finished, clear the list so we can do another 100
requestList.Clear();
}
}
// Make sure that the last batch was awaited so we observe any errors
await Task.WhenAll(requestList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment