使用IAsyncEnumerable读取文本文件 [英] Read text file with IAsyncEnumerable

查看:80
本文介绍了使用IAsyncEnumerable读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在测试C#8.0功能时遇到了IAsyncEnumerable.我从Anthony Chu( https://anthonychu.ca/post/async-streams-dotnet-core-3-iasyncenumerable/).它是异步流,并替代了 Task< IEnumerable< T>>

I came across IAsyncEnumerable while I am testing C# 8.0 features. I found remarkable examples from Anthony Chu (https://anthonychu.ca/post/async-streams-dotnet-core-3-iasyncenumerable/). It is async stream and replacement for 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);
}

我想知道这是否可以应用于读取文本文件,如下面的用法那样逐行读取文件.

I am wondering if this can be applied to read text files like below usage that read file line by line.

foreach (var line in File.ReadLines("Filename"))
{
    // ...process line.
}

我真的很想知道如何将具有 IAsyncEnumerable< string>()的异步应用于上述foreach循环,以便在阅读时流式传输.

I really want to know how to apply async with IAsyncEnumerable<string>() to the above foreach loop so that it streams while reading.

如何实现迭代器,以便可以使用yield return逐行读取?

How do I implement iterator so that I can use yield return to read line by line?

推荐答案

完全一样,但是没有异步工作负载,所以我们假装

Exactly the same, however there is no async workload, so let's pretend

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;
   }
}

这只是打包的APM工作负载,请参阅 Stephen Clearys 注释以进行澄清

This is just an wrapped APM workload, see Stephen Clearys comments for clarification

public static async IAsyncEnumerable<string> SomeSortOfAwesomeness()
{
   using StreamReader reader = File.OpenText("Filename.txt");
   while(!reader.EndOfStream)
      yield return await reader.ReadLineAsync();
}

用法

await foreach(var line in SomeSortOfAwesomeness())
{
   Console.WriteLine(line);
}

更新,来自 Stephen Cleary

File.OpenText 可悲的是只允许同步I/O 异步API 是在这种情况下实施效果不佳.要打开真正的异步文件,您需要使用传递 isAsync FileStream 构造函数:true或 FileOptions.Asynchronous .

File.OpenText sadly only allows synchronous I/O; the async APIs are implemented poorly in that scenario. To open a true asynchronous file, you'd need to use a FileStream constructor passing isAsync: true or FileOptions.Asynchronous.

如您所见,

ReadLineAsync 基本上会生成此代码,就像只有Stream APM的 Begin End 方法包装

ReadLineAsync basically results in this code, as you can see, it's only the Stream APM Begin and End methods wrapped

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
}

这篇关于使用IAsyncEnumerable读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆