Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read text file with IAsyncEnumerable

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

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

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

like image 333
phonemyatt Avatar asked Nov 26 '19 02:11

phonemyatt


People also ask

What's new in iasyncenumerable in ASP NET 6?

One of ASP.NET Core improvements in .NET 6 is support for async streaming of IAsyncEnumerable. In .NET 6, System.Text.Json can serialize incoming IAsyncEnumerable in asynchronous manner.

How to read all texts from the README file into string?

The following shows how to read all texts from the readme.txt file into a string: To read a text file in Python, you follow these steps: First, open a text file for reading by using the open () function. Second, read text from the text file using the file read (), readline (), or readlines () method of the file object.

What is asyncenumerablefactory from async?

AsyncEnumerableFactory. FromAsyncGenerator < int > ( Generator) This library offers a very nice and simple way to express sequences. You build an async function that takes a IAsyncEnumberableSink<T> (defined by the library), and returns a Task.

How to open an asynchronous file in a stream?

To open a true asynchronous file , you'd need to use a FileStream constructor passing isAsync: true or FileOptions.Asynchronous. ReadLineAsync basically results in this code, as you can see, it's only the Stream APM Begin and End methods wrapped


1 Answers

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

or

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

Usage

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

Update from Stephen Cleary

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 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
}
like image 172
TheGeneral Avatar answered Oct 24 '22 15:10

TheGeneral