如何并行运行多个调用thread :: sleep的期货? [英] How to run multiple futures that call thread::sleep in parallel?

查看:68
本文介绍了如何并行运行多个调用thread :: sleep的期货?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的前途很缓慢,会在运行完成之前阻塞1秒钟.

我尝试使用 join 组合器,但是复合期货 my_app 按顺序执行期货:

 #![功能(pin,futures_api,任意_self_types)]板条箱期货//v0.3使用Futures :: prelude :: *;使用futures :: task :: Context;使用std :: pin :: PinMut;使用std :: {thread,time};使用futures :: executor :: ThreadPoolBuilder;struct SlowComputation {}表示SlowComputation的未来{输入Output =();fn民意调查(self:PinMut< Self> ;, _cx:& mut Context)->投票< Self :: Output>{令millis = time :: Duration :: from_millis(1000);thread :: sleep(millis);投票:: Ready(())}}fn main(){让fut1 = SlowComputation {};让fut2 = SlowComputation {};让my_app = fut1.join(fut2);ThreadPoolBuilder :: new().pool_size(5).创造().expect(无法创建线程池").run(my_app);} 

为什么 join 那样工作?我期望期货会在不同的线程上产生.

实现我的目标的正确方法是什么?

Cargo.toml:

  [依赖关系]futures-preview ="0.3.0-alfa.6" 

结果:

  $ time target/debug/futures03真正的0m2.004s用户0m0.000ssys 0分0.004秒 

解决方案

如果将期货与 join()结合使用,它们将被转换为单个任务单线程上.

如果期货表现良好,它们将以事件驱动(异步)方式并行运行.您希望您的应用程序能够休眠1秒钟.

但不幸的是,您实施的未来表现良好.它将当前线程阻塞一秒钟,不允许在此期间执行任何其他工作.因为期货在同一线程上运行,所以它们不能同时运行.您的应用程序将休眠2秒钟.

请注意,如果将示例更改为以下内容,则期货将仍然是单独的任务,您可以在线程池上并行并行地运行它们:

  fn main(){让fut1 = SlowComputation {};让fut2 = SlowComputation {};让mut pool = ThreadPoolBuilder :: new().pool_size(5).创造().expect(无法创建线程池");pool.spawn(fut1);pool.run(fut2);} 

不鼓励使用高度的书面期货,在实际应用中,您可能应该使用图书馆提供的计时器,例如

Why does join work like that? I expected the futures to be spawned on different threads.

What is the right way to obtain my goal?

Cargo.toml:

[dependencies]
futures-preview = "0.3.0-alfa.6"

Result:

$ time target/debug/futures03

real    0m2.004s
user    0m0.000s
sys 0m0.004s

解决方案

If you combine futures with join() they'll be transformed into a single task, running on a single thread.

If the futures are well-behaved, they would run in parallel in an event-driven (asynchronous) manner. You would expect your application to sleep for 1 second.

But unfortunately the future you implemented is not well-behaved. It blocks the current thread for one second, disallowing any other work to be done during this time. Because the futures are run on the same thread, they cannot run at the same time. Your application will sleep for 2 seconds.

Note that if you change your example to the following, the futures will remain separate tasks and you can run them independently in parallel on your thread pool:

fn main() {
    let fut1 = SlowComputation {};
    let fut2 = SlowComputation {};

    let mut pool = ThreadPoolBuilder::new()
        .pool_size(5)
        .create()
        .expect("Failed to create threadpool");

    pool.spawn(fut1);
    pool.run(fut2);
}

Writing futures that block the main thread is highly discouraged and in a real application you should probably use timers provided by a library, for example tokio::timer::Delay or tokio::timer::timeout::Timeout.

这篇关于如何并行运行多个调用thread :: sleep的期货?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆