首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以有限的并发性加入期货

以有限的并发性加入期货
EN

Stack Overflow用户
提问于 2017-04-06 06:20:11
回答 1查看 1.9K关注 0票数 7

我有一个很大的超HTTP请求期货向量,并希望将它们解析为结果向量。由于存在最大打开文件的限制,所以我希望将并发限制为N个期货。

我试验过Stream::buffer_unordered,但它似乎是一个接一个地执行期货。

EN

回答 1

Stack Overflow用户

发布于 2017-04-06 12:59:25

我们在一个项目中使用了这样的代码,以避免打开太多的TCP套接字。这些期货里面有超期货,所以情况似乎完全一样。

代码语言:javascript
复制
// Convert the iterator into a `Stream`. We will process
// `PARALLELISM` futures at the same time, but with no specified
// order.
let all_done =
    futures::stream::iter(iterator_of_futures.map(Ok))
    .buffer_unordered(PARALLELISM);

// Everything after here is just using the stream in
// some manner, not directly related

let mut successes = Vec::with_capacity(LIMIT);
let mut failures = Vec::with_capacity(LIMIT);

// Pull values off the stream, dividing them into success and
// failure buckets.
let mut all_done = all_done.into_future();
loop {
    match core.run(all_done) {
        Ok((None, _)) => break,
        Ok((Some(v), next_all_done)) => {
            successes.push(v);
            all_done = next_all_done.into_future();
        }
        Err((v, next_all_done)) => {
            failures.push(v);
            all_done = next_all_done.into_future();
        }
    }
}

这在一段示例代码中使用,因此事件循环(core)是显式驱动的。查看该程序使用的文件句柄的数量显示它是有上限的。此外,在添加此瓶颈之前,我们很快就用完了允许的文件句柄,而之后却没有。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43247212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档