1

我想在foo执行器内部生成一个任务current_thread

extern crate futures; // version 0.1
extern crate tokio;

use futures::future::lazy;
use std::{thread, time};
use tokio::executor::current_thread::CurrentThread;

fn foo() {
    tokio::spawn(lazy(|| {
        println!("In foo!");
        Ok(())
    }));
}

fn main() {
    let mut current_thread = CurrentThread::new();
    current_thread.spawn(lazy(|| {
        loop {
            println!("In spawn!");
            foo();
            thread::sleep(time::Duration::from_secs(1));
        }
        Ok(())
    }));
    current_thread.run().unwrap();
}

当我运行它时,它失败了:

In spawn!
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SpawnError { is_shutdown: true }', libcore/result.rs:945:5
stack backtrace:
   0:        0x10879438f - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::hd4a234eb73c9eb11
                               at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1:        0x108788b9d - std::sys_common::backtrace::print::h686165a0aebbb1f5
                               at libstd/sys_common/backtrace.rs:71
                               at libstd/sys_common/backtrace.rs:59
   2:        0x108797043 - std::panicking::default_hook::{{closure}}::h25aacbcb8a8273d9
                               at libstd/panicking.rs:205
   3:        0x108796dcc - std::panicking::default_hook::h036425f579a26a8f
                               at libstd/panicking.rs:221
   4:        0x10879758b - <std::panicking::begin_panic::PanicPayload<A> as core::panic::BoxMeUp>::get::hac9fda42d0999bf5
                               at libstd/panicking.rs:457
   5:        0x1087971c0 - std::panicking::try::do_call::h8b3a10ecb1a21334
                               at libstd/panicking.rs:344
   6:        0x108797182 - std::panicking::try::do_call::h8b3a10ecb1a21334
                               at libstd/panicking.rs:322
   7:        0x1087cbac5 - <&'a T as core::fmt::Display>::fmt::h2e38aa9ebf1dda6d
                               at libcore/panicking.rs:71
   8:        0x108771405 - core::result::unwrap_failed::h111a0aed60721615
                               at /Users/travis/build/rust-lang/rust/src/libcore/macros.rs:26
   9:        0x10877110b - <core::result::Result<T, E>>::unwrap::h1b36496d5b4f78f0
                               at /Users/travis/build/rust-lang/rust/src/libcore/result.rs:782
  10:        0x108771ba6 - tokio_executor::global::spawn::h95932f8a5d0c0373
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-executor-0.1.2/src/global.rs:128
  11:        0x108771b26 - tokio::executor::spawn::hbdc3654dcb691a15
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/mod.rs:207
  12:        0x108772a5a - ox::foo::h1a20773fe06f38f5
                               at src/main.rs:9
  13:        0x1087729e3 - ox::main::{{closure}}::ha045cd464fd2bcd2
                               at src/main.rs:20
  14:        0x108770de0 - <futures::future::lazy::Lazy<F, R>>::get::h6d01f86e63eb0ae6
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/future/lazy.rs:64
  15:        0x108770f54 - <futures::future::lazy::Lazy<F, R> as futures::future::Future>::poll::he4dbb0cba4734268
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/future/lazy.rs:82
  16:        0x1087765a4 - core::fmt::Arguments::new_v1_formatted::h535b215c9c95b52a
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/future/mod.rs:113
  17:        0x1087750a4 - <futures::task_impl::Spawn<T>>::poll_future_notify::{{closure}}::he965765a8e172c4b
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/task_impl/mod.rs:289
  18:        0x10877520a - <futures::task_impl::Spawn<T>>::enter::{{closure}}::hc5d84c8a0c9b9541
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/task_impl/mod.rs:363
  19:        0x108774dc8 - futures::task_impl::std::set::h6d292679eb1abcd6
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/task_impl/std/mod.rs:78
  20:        0x1087751a7 - <futures::task_impl::Spawn<T>>::enter::h19b2ea99bdf79995
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/task_impl/mod.rs:363
  21:        0x108775039 - <futures::task_impl::Spawn<T>>::poll_future_notify::hf216aece3ea15c32
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/futures-0.1.21/src/task_impl/mod.rs:289
  22:        0x108773e03 - <tokio::executor::current_thread::scheduler::Scheduled<'a, U>>::tick::haebb55271ba68f7f
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/scheduler.rs:352
  23:        0x108773d8c - <tokio::executor::current_thread::scheduler::Scheduler<U>>::tick::{{closure}}::h0fdf80d08ed0341a
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/scheduler.rs:332
  24:        0x1087720fc - <tokio::executor::current_thread::Borrow<'a, U>>::enter::{{closure}}::{{closure}}::hc3a2b045d8dbd69f
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:688
  25:        0x108771c7f - tokio::executor::current_thread::CurrentRunner::set_spawn::h796df298bd44491f
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:720
  26:        0x10877209c - <tokio::executor::current_thread::Borrow<'a, U>>::enter::{{closure}}::hf2160ea4bc6cb70d
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:687
  27:        0x108771887 - <std::thread::local::LocalKey<T>>::try_with::h07c3b54d23ac7bcf
                               at /Users/travis/build/rust-lang/rust/src/libstd/thread/local.rs:294
  28:        0x1087716b4 - <std::thread::local::LocalKey<T>>::with::h8a95d51541a57481
                               at /Users/travis/build/rust-lang/rust/src/libstd/thread/local.rs:248
  29:        0x108771fda - <tokio::executor::current_thread::Borrow<'a, U>>::enter::hd84e35a45b8c5f10
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:686
  30:        0x108773c6d - <tokio::executor::current_thread::scheduler::Scheduler<U>>::tick::hc6a4d68c4ca62da1
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/scheduler.rs:332
  31:        0x1087724bd - <tokio::executor::current_thread::Entered<'a, P>>::tick::h806caa14d1fda0e2
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:572
  32:        0x1087721ac - <tokio::executor::current_thread::Entered<'a, P>>::run_timeout2::hacdc210a9580db1b
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:541
  33:        0x108772450 - <tokio::executor::current_thread::Entered<'a, P>>::run::h98ebc98ef8f09a85
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:484
  34:        0x108771e04 - <tokio::executor::current_thread::CurrentThread<P>>::run::h023b6fbff627efb0
                               at /tmp/registry/src/github.com-1ecc6299db9ec823/tokio-0.1.6/src/executor/current_thread/mod.rs:356
  35:        0x108772ac0 - ox::main::h5fd6c3ac8eb3383f
                               at src/main.rs:25
  36:        0x108772cc1 - std::rt::lang_start::{{closure}}::h8d7d3864a0a638c3
                               at /Users/travis/build/rust-lang/rust/src/libstd/rt.rs:74
  37:        0x1087970e7 - std::panicking::try::do_call::h8b3a10ecb1a21334
                               at libstd/rt.rs:59
                               at libstd/panicking.rs:304
  38:        0x1087a2cde - panic_unwind::dwarf::eh::read_encoded_pointer::hcabc31a4d46081c4
                               at libpanic_unwind/lib.rs:105
  39:        0x10878fd00 - std::sys_common::bytestring::debug_fmt_bytestring::h020ca4453b64bdbd
                               at libstd/panicking.rs:283
                               at libstd/panic.rs:361
                               at libstd/rt.rs:58
  40:        0x108772ca1 - std::rt::lang_start::h14c96d0aa9f67dbd
                               at /Users/travis/build/rust-lang/rust/src/libstd/rt.rs:74
  41:        0x108772b14 - ox::main::h5fd6c3ac8eb3383f

我尝试传递current_threadfoo但无法编译。是否有可能做到这一点,如果可以,怎么做?

4

1 回答 1

4

对的,这是可能的。Tokio 提供了有关如何使用的文档current_thread

use futures::future::lazy;

// Calling execute here results in a panic
// current_thread::spawn(my_future);

current_thread::block_on_all(lazy(|| {
    // The execution context is setup, futures may be executed.
    current_thread::spawn(lazy(|| {
        println!("called from the current thread executor");
        Ok(())
    }));

    Ok::<_, ()>(())
}));

您的具体问题是您没有在当前线程上生成任务:

fn foo() {
    tokio::spawn(lazy(|| {
//  ^^^^^^^^^^^^

相反,这应该是

fn foo() {
    current_thread::spawn(lazy(|| {
//  ^^^^^^^^^^^^^^^^^^^^^

但是,我不相信这段代码会真正打印出来In foo!,因为外部循环永远不会创建一个会阻塞的未来,因此执行者没有理由查看任何其他未来。

于 2018-05-06T11:44:36.323 回答