pho*_*att 7 c# system.io.file c#-8.0 iasyncenumerable
我在测试 C# 8.0 功能时遇到了 IAsyncEnumerable。我从 Anthony Chu ( https://anthonychu.ca/post/async-streams-dotnet-core-3-iasyncenumerable/ ) 中找到了非凡的例子。它是异步流和替代Task<IEnumerable<T>>
// Data Access Layer.
public async IAsyncEnumerable<Product> GetAllProducts()
{
Container container = cosmosClient.GetContainer(DatabaseId, ContainerId);
var iterator = container.GetItemQueryIterator<Product>("SELECT * FROM c");
while (iterator.HasMoreResults)
{
foreach (var product in await iterator.ReadNextAsync())
{
yield return product;
}
}
}
// Usage
await foreach (var product in productsRepository.GetAllProducts())
{
Console.WriteLine(product);
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是否可以应用于读取文本文件,例如下面逐行读取文件的用法。
foreach (var line in File.ReadLines("Filename"))
{
// ...process line.
}
Run Code Online (Sandbox Code Playgroud)
我真的很想知道如何将 async withIAsyncEnumerable<string>()应用于上述 foreach 循环,以便在阅读时进行流式传输。
如何实现迭代器以便我可以使用 yield return 逐行读取?
完全相同,但是没有异步工作负载,所以让我们假设
public async IAsyncEnumerable<string> SomeSortOfAwesomeness()
{
foreach (var line in File.ReadLines("Filename.txt"))
{
// simulates an async workload,
// otherwise why would be using IAsyncEnumerable?
// -- added due to popular demand
await Task.Delay(100);
yield return line;
}
}
Run Code Online (Sandbox Code Playgroud)
或者
这只是一个打包的 APM 工作负载,请参阅Stephen Clearys 的评论以获取说明
public static async IAsyncEnumerable<string> SomeSortOfAwesomeness()
{
using StreamReader reader = File.OpenText("Filename.txt");
while(!reader.EndOfStream)
yield return await reader.ReadLineAsync();
}
Run Code Online (Sandbox Code Playgroud)
用法
await foreach(var line in SomeSortOfAwesomeness())
{
Console.WriteLine(line);
}
Run Code Online (Sandbox Code Playgroud)
斯蒂芬·克利里的更新
File.OpenText遗憾的是只允许同步 I/O;在这种情况下,异步API 的实现很差。要打开真正的异步文件,您需要使用FileStream构造函数传递isAsync: true 或FileOptions.Asynchronous.
ReadLineAsync基本上生成此代码,如您所见,它只是 Stream APMBegin和End包装的方法
private Task<Int32> BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
{
return TaskFactory<Int32>.FromAsyncTrim(
this, new ReadWriteParameters { Buffer = buffer, Offset = offset, Count = count },
(stream, args, callback, state) => stream.BeginRead(args.Buffer, args.Offset, args.Count, callback, state), // cached by compiler
(stream, asyncResult) => stream.EndRead(asyncResult)); // cached by compiler
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1792 次 |
| 最近记录: |