- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
Method(String [] Strs)-6ren">
常规迭代器 block (即“yield return”)是否与“async”和“await”不兼容?
这很好地说明了我要做什么:
async Task<IEnumerable<Foo>> Method(String [] Strs)
{
// I want to compose the single result to the final result, so I use the SelectMany
var finalResult = UrlStrings.SelectMany(link => //i have an Urlstring Collection
await UrlString.DownLoadHtmlAsync() //download single result; DownLoadHtmlAsync method will Download the url's html code
);
return finalResult;
}
但是,我收到一个编译器错误,指出“无法从资源中加载消息字符串”。
这是另一种尝试:
async Task<IEnumerable<Foo>> Method(String [] Strs)
{
foreach(var str in strs)
{
yield return await DoSomethingAsync( str)
}
}
但是,编译器再次返回错误:“无法从资源加载消息字符串”。
这是我项目中真正的编程代码
这在我有一个列表任务时非常有用,该任务可以从 URL 下载 HTML我使用语法“yield return await task”,结果是我想要 IEnumerable<Foo>
.我不想写这段代码:
async Task<IEnumerable<String>> DownLoadAllURL(String [] Strs)
{
List<Foo> htmls= new ...
foreach(var str in strs)
{
var html= await DownLoadHtmlAsync( str)
htmls.Add(item)
}
return htmls;
}
但看来我不得不这样做。
感谢您的帮助。
最佳答案
您所描述的内容可以通过 Task.WhenAll
方法来完成。请注意代码如何变成一个简单的单行代码。发生的情况是每个单独的 url 开始下载,然后使用 WhenAll
将这些操作组合到一个可以等待的 Task
中。
Task<IEnumerable<string>> DownLoadAllUrls(string[] urls)
{
return Task.WhenAll(from url in urls select DownloadHtmlAsync(url));
}
关于c# - 有没有可能到 "await yield return DoSomethingAsync()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5061761/
function* generatorFunction() { yield (yield 1)(yield 2)(yield 3)(); } var iterator = generatorFun
ECMAScript 6 应该带来生成器函数和迭代器。生成器函数(具有 function* 语法)返回一个迭代器。迭代器有一个 next 方法,当重复调用时,该方法会执行生成器函数的主体,并在每个 y
ECMAScript 6 应该引入生成器函数和迭代器。生成器函数(具有 function* 语法)返回迭代器。迭代器有一个 next 方法,当重复调用时,它会执行生成器函数的主体,在每个 yield
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 2 年前。 Improve t
自 python 2.5 以来,可以将 send()、throw()、close() 放入生成器中。在定义的生成器中,可以通过执行以下操作来“捕获”发送的数据: def gen(): whil
return的区别和 yield似乎很清楚,直到我发现还有 yield from以及将两者结合起来的可能性 return和 yield在完全相同的功能! 我对return的理解之后的一切都是 不是 执
假设我有这个部分,我正在尝试渲染 #layouts/_subheader.html.erb 当我在这样的 View 中使用这个部分时 Content For Yield
yield操作符是由编译器在底层实现的,该编译器生成一个实现符合 IEnumerable 的状态机的类。和 IEnumerator . 给定一个罗斯林 MethodDeclarationSyntax
$item) echo "$index $item" . PHP_EOL; } resolve(generator1()); echo PHP_EOL; resolve(gener
这个问题在这里已经有了答案: Why converting list to set is faster than converting generator to set? (1 个回答) List c
是否有一个单行代码来获取生成器并生成该生成器中的所有元素?例如: def Yearly(year): yield YEARLY_HEADER for month in range(1, 13)
刚发现yield from 结构,在我看来这有点像反向的yield,而不是从生成器中获取对象,您插入/将对象发送到生成器。喜欢: def foo(): while True:
考虑以下代码: def mygen(): yield (yield 1) a = mygen() print(next(a)) print(next(a)) 输出产量: 1 None 解释器
Guido van Rossum,在 2014 年关于 Tulip/Asyncio 的演讲中 shows the slide : Tasks vs coroutines Compare: res =
谁能帮我理解“yield self”和“yield”的区别? class YieldFirstLast attr_accessor :first, :last def initiali
这是我目前使用 Laravel 5 实现的 Open Graph 标签: app.blade.php @yield('title') page.blade.php @extends('app'
在 Tornado 中,我们通常会编写如下代码来异步调用函数: class MainHandler(tornado.web.RequestHandler): @tornado.gen.coro
本文整理了Java中aQute.bnd.indexer.analyzers.Yield.yield()方法的一些代码示例,展示了Yield.yield()的具体用法。这些代码示例主要来源于Github
我们有超过 100 个共同基金的每日返回,我们希望将这些返回转换为月度返回。每月返回不应是每个月的平均值,而是每个月末的资金返回。基金在不同的时间点开始和结束,它们需要自己保留(不是每个月的共同基金
如何实现 C# yield return使用 Scala 延续?我希望能够编写 Scala Iterator s 风格相同。在 this Scala news post 的评论中有刺伤,但它不起作用(
我是一名优秀的程序员,十分优秀!