admin管理员组文章数量:1410674
In the examples for using the bulk support, the tasks created by the SDK are awaited as such:
List<Task> concurrentTasks = new List<Task>();
foreach(Item itemToInsert in ReadYourData())
{
concurrentTasks.Add(container.CreateItemAsync(itemToInsert, new PartitionKey(itemToInsert.MyPk)));
}
await Task.WhenAll(concurrentTasks);
In a more real world scenario, there might be a lot more await/async going on. Developers often awaits tasks many levels of deep, although no IO takes place. Will the SDK be able to identify its tasks it needs to use the bulk support in a scenario like this, or will they be left to their own demise and execute individually?
private async Task StartHere()
{
var articles = new List<Article>() {10k articles};
var saveTasks = articles.Select(async a => await Save(a));
await Task.WhenAll(saveTasks); // Will this still use the bulk support?
}
private async Task<Article> Save(Article article)
{
await Upsert(article, CancellationToken.None);
return article;
}
private async Task Upsert(Article article, CancellationToken cancellationToken)
{
await InsertOrUpdate(article, cancellationToken);
}
private async Task<string> InsertOrUpdate(T entity, CancellationToken cancellationToken)
{
var partitionKey = new PartitionKey(entity.Id);
await _container.UpsertItemAsync(entity, partitionKey, new ItemRequestOptions
{
EnableContentResponseOnWrite = false,
IfMatchEtag = entity.ETag
}, cancellationToken: cancellationToken);
return entity.Id;
}
In the examples for using the bulk support, the tasks created by the SDK are awaited as such:
List<Task> concurrentTasks = new List<Task>();
foreach(Item itemToInsert in ReadYourData())
{
concurrentTasks.Add(container.CreateItemAsync(itemToInsert, new PartitionKey(itemToInsert.MyPk)));
}
await Task.WhenAll(concurrentTasks);
In a more real world scenario, there might be a lot more await/async going on. Developers often awaits tasks many levels of deep, although no IO takes place. Will the SDK be able to identify its tasks it needs to use the bulk support in a scenario like this, or will they be left to their own demise and execute individually?
private async Task StartHere()
{
var articles = new List<Article>() {10k articles};
var saveTasks = articles.Select(async a => await Save(a));
await Task.WhenAll(saveTasks); // Will this still use the bulk support?
}
private async Task<Article> Save(Article article)
{
await Upsert(article, CancellationToken.None);
return article;
}
private async Task Upsert(Article article, CancellationToken cancellationToken)
{
await InsertOrUpdate(article, cancellationToken);
}
private async Task<string> InsertOrUpdate(T entity, CancellationToken cancellationToken)
{
var partitionKey = new PartitionKey(entity.Id);
await _container.UpsertItemAsync(entity, partitionKey, new ItemRequestOptions
{
EnableContentResponseOnWrite = false,
IfMatchEtag = entity.ETag
}, cancellationToken: cancellationToken);
return entity.Id;
}
Share
Improve this question
edited Mar 6 at 8:01
David Makogon
70.9k22 gold badges145 silver badges198 bronze badges
asked Mar 6 at 7:26
spanspan
5,62811 gold badges67 silver badges119 bronze badges
1 Answer
Reset to default 2It doesn't matter how you call the methods. You can basically think of it in the following (simplified) way; There's a queue where all your requests are gathered. Once it's big enough for a batch or after a certain delay it'll pick up the requests and send them as a batch.
All of it is managed by the SDK so whether you use async/await
in one way, another, or not at all doesn't matter.
本文标签: cHow does the net CosmosDb SDK handle tasks in bulk supportStack Overflow
版权声明:本文标题:c# - How does the .net CosmosDb SDK handle tasks in bulk support? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744992110a2636447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论