本文我将回顾分享

  • foreach/yield return/async await语法糖的本质
  • 如何使用异步流
  • 附加探索: 编写一个更有意义的迭代效果

foreach/ yield return/async await的本质
.net诞生之初,就通过ienumerable、ienumerator提供迭代能力, 前者代表具备可枚举的性质,后者代表可被枚举的方式。
如果你真的使用强类型ienumerable/ienumerator来产生/消费可枚举类型,会发现要写很多琐碎代码。

c#推出的yield return迭代器语法糖,简化了产生可枚举类型的编写过程。(编译器将yield return转换为状态机代码来实现ienumerable,ienumerator)

yield 关键字可以执行状态迭代,并逐个返回枚举元素,在返回数据时,无需创建临时集合来存储数据。

c#foreach语法糖,简化了消费可枚举类型的编写过程。(编译器将foreach抓换为强类型的方法/属性调用)

ienumerable src = ...;
ienumerator e = src.getenumerator();
try
{
  while (e.movenext()) use(e.current);
}
finally { if (e != null) e.dispose(); }

net framework4引入task,.net framework 4.5/c#5.0引入了await/async异步编程语法糖,简化了异步的编写过程。(编译器将await/async语法糖转换为状态机,产生task并在内部回调)

️以上也看出微软为帮助我们更快速优雅地编写代码,给了很多糖,编译器做了很多事情。

c#提供了迭代、异步的快捷方式,能否将两者结合?
两者结合的效果就是:我们希望在数据就绪时,接收并处理数据,但不会以阻塞cpu的形式等待,这在lot流式数据中很常见。

异步迭代

有一只爬虫要通过列表页上的链接,抓取链接背后的html内容并显示。

这是一个[相互独立的长耗时行为的集合(假设分别耗时5,4,3,2,1s)],
我们使用c#8.0异步可枚举类型iasyncenumerable,异步 产生/消费枚举元素。

与同步版本iemunerable类似,iasyncenumerable也有对应的iasyncenumerator迭代器,迭代器的实现过程决定了foreach消费的顺序。

c#8.0  asynchronous streams

c#8.0中一个重要的特性是异步流(async stream), 可以轻松创建和消费异步枚举。

返回异步流的方法特征:

  • 以async修饰符声明
  • 返回iasyncenumerable<t>对象
  • 方法包含yield return语句,用来异步持续返回元素
static async task main(string[] args)
{
      console.writeline(datetime.now + $"\tthreadid:{thread.currentthread.managedthreadid}\r\n");

      await foreach (var html in fetchallhtml())
      {
           console.writeline(datetime.now + $"\tthreadid:{thread.currentthread.managedthreadid}\t" + $"\toutput:{html}");
      }
      console.writeline("\r\n" + datetime.now + $"\tthreadid:{thread.currentthread.managedthreadid}\t");
      console.readkey();
 }

 static async iasyncenumerable<string> fetchallhtml()
 {
    for (int i = 5; i >= 1; i--)
    {
        var html = await task.delay(i* 1000).continuewith((t,i)=> $"html{i}",i);    //  模拟长耗时
        yield return html;
    }
 }

for循环结合yield关键字,决定了iasyncenumerator的实现;
以上代码将使得await foreach消费异步枚举时, 采用与for循环一样的顺序,也就是产生异步任务的先后顺序。

以上不会等待15s然后一股脑抛出所有数据, 而是根据枚举for循环  依次就绪,依次显示,总共还是耗时15s,每一次枚举都是异步的。

附加思考:产生一个有意思的迭代器

️ 但是我内心想,能不能按照完成异步任务的顺序,先完成先消费,这难道不是人之常情,交互体验应该更好。

static async iasyncenumerable<string> fetchallhtml()
{  
    var tasklist= new list<task<string>>();
    for (int i = 5; i >= 1; i--)
    {
        var t= task.delay(i* 1000).continuewith((t,i)=>$"html{i}",i);      // 模拟长耗时任务
        tasklist.add(t);
    }
     while(tasklist.any())  
    {
       var tfinlish = await task.whenany(tasklist);
       tasklist.remove(tfinlish); 
       yield return await tfinlish;
    }
}

上面我先构造了可等待的任务列表,通过task.whenany() 返回异步任务先完成的迭代元素。  

以上总耗时取决于 耗时最长的那个枚举任务:5s
.netcore 3.1 已经可以在webapi中使用异步流,意味着我们可将流式数据返回到http响应。

前端也已经有试验性的streams api可以消费流式数据。

传送门:   https://developer.mozilla.org/en-us/docs/web/api/streams_api
浏览器兼容列表:    https://developer.mozilla.org/en-us/docs/web/api/streams_api#browser_compatibility

对于web应用,这着实能提高 可交互性:
想象之前含多个长耗时行为的列表数据,现在不必等待所有数据,配以loading,谁先完成谁加载,效果杠杠。

以上就是c# async streams的使用解析的详细内容,更多关于c# async streams的使用的资料请关注www.887551.com其它相关文章!