@GeraZlo

Почему происходит ошибка в reqwest, если не использовать tokio::main?

Если использовать #[tokio::main] то код работает успешно:

Код
use futures::executor::LocalPool;
use futures::task::LocalSpawnExt;

async fn test(n: i32) {
    println!("Started: {}", n);
    let response: String = reqwest::get("https://www.rust-lang.org")
        .await
        .unwrap()
        .text()
        .await
        .unwrap();
    println!("Completed: {}. Response: {}", n, &response[0..10]);
}

#[tokio::main]
async fn main() {
    let mut pool = LocalPool::new();
    let mut spawner = pool.spawner();

    for i in 0..5 {
        spawner.spawn_local(test(i)).unwrap();
    }
    pool.run();
}


Но если убрать, то возникает ошибка:
Console output

Started: 0
Started: 1
Started: 2
Started: 3
Started: 4
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: reqwest::Error { kind: Request, url: "https://www.rust-lang.org/", source: Error(Connect, Custom { kind: Other, error: "no current reactor" }) }', src/libcore/result.rs:1165:5
stack backtrace:
0: backtrace::backtrace::libunwind::trace
at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
1: backtrace::backtrace::trace_unsynchronized
at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
2: std::sys_common::backtrace::_print_fmt
at src/libstd/sys_common/backtrace.rs:77
3: ::fmt
at src/libstd/sys_common/backtrace.rs:61
4: core::fmt::write
at src/libcore/fmt/mod.rs:1028
5: std::io::Write::write_fmt
at src/libstd/io/mod.rs:1412
6: std::sys_common::backtrace::_print
at src/libstd/sys_common/backtrace.rs:65
7: std::sys_common::backtrace::print
at src/libstd/sys_common/backtrace.rs:50
8: std::panicking::default_hook::{{closure}}
at src/libstd/panicking.rs:188
9: std::panicking::default_hook
at src/libstd/panicking.rs:205
10: std::panicking::rust_panic_with_hook
at src/libstd/panicking.rs:464
11: std::panicking::continue_panic_fmt
at src/libstd/panicking.rs:373
12: rust_begin_unwind
at src/libstd/panicking.rs:302
13: core::panicking::panic_fmt
at src/libcore/panicking.rs:139
14: core::result::unwrap_failed
at src/libcore/result.rs:1165
15: core::result::Result::unwrap
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libcore/result.rs:933
16: hello_world::test::{{closure}}
at src/main.rs:6
17: as core::future::future::Future>::poll::{{closure}}
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libstd/future.rs:43
18: std::future::set_task_context
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libstd/future.rs:79
19: as core::future::future::Future>::poll
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libstd/future.rs:43
20: as core::future::future::Future>::poll
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-task-0.3.0/src/future_obj.rs:86
21: as futures_core::stream::Stream>::poll_next
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.0/src/stream/futures_unordered/mod.rs:434
22: futures_util::stream::stream::StreamExt::poll_next_unpin
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.0/src/stream/stream/mod.rs:1149
23: futures_executor::local_pool::LocalPool::poll_pool_once
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-executor-0.3.0/src/local_pool.rs:269
24: futures_executor::local_pool::LocalPool::poll_pool
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-executor-0.3.0/src/local_pool.rs:242
25: futures_executor::local_pool::LocalPool::run::{{closure}}
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-executor-0.3.0/src/local_pool.rs:121
26: futures_executor::local_pool::run_executor::{{closure}}
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-executor-0.3.0/src/local_pool.rs:69
27: std::thread::local::LocalKey::try_with
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libstd/thread/local.rs:262
28: std::thread::local::LocalKey::with
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libstd/thread/local.rs:239
29: futures_executor::local_pool::run_executor
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-executor-0.3.0/src/local_pool.rs:65
30: futures_executor::local_pool::LocalPool::run
at /home/strix/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-executor-0.3.0/src/local_pool.rs:121
31: hello_world::main
at src/main.rs:22
32: std::rt::lang_start::{{closure}}
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libstd/rt.rs:61
33: std::rt::lang_start_internal::{{closure}}
at src/libstd/rt.rs:48
34: std::panicking::try::do_call
at src/libstd/panicking.rs:287
35: __rust_maybe_catch_panic
at src/libpanic_unwind/lib.rs:79
36: std::panicking::try
at src/libstd/panicking.rs:265
37: std::panic::catch_unwind
at src/libstd/panic.rs:396
38: std::rt::lang_start_internal
at src/libstd/rt.rs:47
39: std::rt::lang_start
at /rustc/38048763e885a3ee139abf39d59a530b16484150/src/libstd/rt.rs:61
40: main
41: __libc_start_main
42: _start
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


Почему и зачем нужен tokio::main, ведь я в качестве ранера использую LocalPool?
  • Вопрос задан
  • 56 просмотров
Пригласить эксперта
Ответы на вопрос 1
@GeraZlo Автор вопроса
Окей, видимо я сам туплю, внимательнее прочитал ошибку, для работу нужен некий reactor.

https://docs.rs/tokio/0.1.22/tokio/reactor/index.html
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы