Skip to Content
C#并发编程经典实例(第2版)
book

C#并发编程经典实例(第2版)

by Stephen Cleary
November 2020
Intermediate to advanced
226 pages
5h 34m
Chinese
Posts & Telecom Press
Content preview from C#并发编程经典实例(第2版)
38
3
代表某些
I/O
密集型操作,比如数据库查询或
API
调用,那么消费者代码最终会阻塞
I/O
这并不理想。
IAsyncEnumerable<T>
的工作方式与
IEnumerable<T>
类似,但是它会异步获取
下一个元素。
3.3
 异步流和
Task<IEnumerable<T>>
异步返回的集合完全可以包含多个数据项,
Task<List<T>>
就是一个常见的例子。返
List<T>
async
方法仍旧只有一条
return
语句,在将集合返回之前必须将其彻底填
充。即使是返回
Task<IEnumerable<T>>
的方法,也能够异步返回枚举,但随后该枚举将
被同步计算。注意,在
LINQ to Entities
中有一个
ToListAsync
LINQ
方法,该方法返回
Task<List<T>>
。当
LINQ
提供程序执行此操作时,它必须与数据库通信,并在完成填充列
表和返回它之前获取所有匹配的响应。
Task<IEnumerable<T>>
的局限在于无法返回刚获取的数据项。如果返回集合,那么它必
须把所有项都加载到内存中,填充该集合,然后一次性返回整个集合。即便它返回
LINQ
查询,并且可以异步构建该查询,但一经返回,该查询中的每一项也是同步获取的。
IAsyncEnumerable<T>
也会异步返回多个数据项,但区别在于,
IAsyncEnumerable<T>
可以
针对返回的每一项执行异步操作,这是真正由数据项组成的异步流。
3.4
 异步流和
IObservable<T>
可观察对象是真正的异步流概念 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

解密金融数据

解密金融数据

Justin Pauley
PHP编程:第4版

PHP编程:第4版

Kevin Tatroe, Peter MacIntyre

Publisher Resources

ISBN: 9787115550606