rust - 如何解决错误 "thread ' main' paniced at 'no current reactor'”?

标签 rust future rust-tokio

我正在尝试连接到数据库:

extern crate tokio; // 0.2.6, features = ["full"]
extern crate tokio_postgres; // 0.5.1

use futures::executor;
use tokio_postgres::NoTls;

fn main() {
    let fut = async {
        let (client, connection) = match tokio_postgres::connect("actual stuff", NoTls).await {
            Ok((client, connection)) => (client, connection),
            Err(e) => panic!(e),
        };
        tokio::spawn(async move {
            if let Err(e) = connection.await {
                eprintln!("connection error: {}", e);
            }
        });

        let rows = match client
            .query(
                "SELECT $1 FROM planet_osm_point WHERE $1 IS NOT NULL LIMIT 100",
                &[&"name"],
            )
            .await
        {
            Ok(rows) => rows,
            Err(e) => panic!(e),
        };
        let names: &str = rows[0].get("name");
        println!("{:?}", names);
    };
    executor::block_on(fut);
    println!("Hello, world!");
}

它编译了,但是当我运行它时,我收到了错误消息

thread 'main' panicked at 'no current reactor'

最佳答案

在使用许多(但不是全部)Tokio 功能时,您必须使用 Tokio reactor。在您的代码中,您尝试使用 future 箱 ( executor::block_on ) 提供的通用执行程序。使用 Tokio 执行器和 react 器通常是通过使用 #[tokio::main] 来完成的。宏:

#[tokio::main]
async fn main() {
    let (client, connection) = match tokio_postgres::connect("actual stuff", NoTls).await {
        Ok((client, connection)) => (client, connection),
        Err(e) => panic!(e),
    };
    tokio::spawn(async move {
        if let Err(e) = connection.await {
            eprintln!("connection error: {}", e);
        }
    });

    let rows = match client
        .query(
            "SELECT $1 FROM planet_osm_point WHERE $1 IS NOT NULL LIMIT 100",
            &[&"name"],
        )
        .await
    {
        Ok(rows) => rows,
        Err(e) => panic!(e),
    };
    let names: &str = rows[0].get("name");
    println!("{:?}", names);
}

the tokio_postgres docs 中的第一个例子甚至向您展示如何做到这一点:

#[tokio::main] // By default, tokio_postgres uses the tokio crate as its runtime.
async fn main() -> Result<(), Error> {


发生这种情况的一个原因是因为您正在使用 tokio::spawn 其中has this documented :

Panics if called from outside of the Tokio runtime.



也可以看看:
  • How do I synchronously return a value calculated in an asynchronous Future in stable Rust?


  • 这不会打印您想要的内容:
    Err(e) => panic!(e),
    

    你要
    Err(e) => panic!("{}", e),
    

    关于rust - 如何解决错误 "thread ' main' paniced at 'no current reactor'”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59582398/

    相关文章:

    rust - 有没有一种方法可以简化没有宏将Option转换为Result的方法?

    java - 在这方面,Future 和 Completable future 有什么区别?

    rust - 如何在 Rust 中的不同 Future 特征之间创建互操作性?

    rust - 如何创建一个 tokio 专用传输来覆盖默认的 tick 实现?

    rust - 如何在 Rust 中通过借用来正确管理所有权?

    rust - (Rust) 有没有办法为重载的运算符自动借用?

    scala 全局 ExecutionContext 与 ExecutionContextExecutorService

    asynchronous - 如何使用 tokio::join 有条件地运行两个函数之一?

    rust - 在 Rust 中插入向量的复杂性是什么?

    R:在 `future::plan(sequential)` 中使用 `browser()` 时如何启用打印到控制台?