Async with huge data streams

后端 未结 5 1092
执念已碎
执念已碎 2020-12-13 00:16

We use IEnumerables to return huge datasets from database:

public IEnumerable Read(...)
{
    using(var connection = new SqlConnection(...))
             


        
5条回答
  •  甜味超标
    2020-12-13 00:38

    The easiest option is using TPL Dataflow. All you need to do is configure an ActionBlock that handles the processing (in parallel if you wish) and "sends" the items into it one by one asynchronously.
    I would also suggest setting a BoundedCapacity which will throttle the reader reading from the database when the processing can't handle the speed.

    var block = new ActionBlock(
        data => ProcessDataAsync(data),
        new ExecutionDataflowBlockOptions
        {
            BoundedCapacity = 1000,
            MaxDegreeOfParallelism = Environment.ProcessorCount
        });
    
    using(var connection = new SqlConnection(...))
    {
        // ...
        while(await reader.ReadAsync().ConfigureAwait(false))
        {
            // ...
           await block.SendAsync(item);
        }
    }
    

    You can also use Reactive Extensions, but that's a more complicated and robust framework than you probably need.

提交回复
热议问题