繁体   English   中英

如何在 tokio_postgres 中使用自定义 Tokio 运行时(并且没有 tokio::main 宏)?

[英]How do I use a custom Tokio runtime within tokio_postgres (and without the tokio::main macro)?

我将如何使用自定义 tokio 运行时构建器而不使用主宏来实现这个 tokio_postgres 示例?

根据 tokio_postgres 文档,这很好用:

例子/withmacro.rs

use tokio_postgres::{NoTls, Error};

async fn db_main()-> Result<(), Error> {
    
    // pasted from: https://docs.rs/tokio-postgres/0.6.0/tokio_postgres/index.html
    
    // Connect to the database.
    let conn_string = std::env::var("PG_CONNECT").unwrap();
    let (client, connection) = tokio_postgres::connect(&conn_string,NoTls).await?;
    
    // The connection object performs the actual communication
    tokio::spawn(async move {
        if let Err(e) = connection.await {
            eprintln!("connection error: {}", e);
        }
    });

    // Now we can execute a simple statement that just returns its parameter.
    let rows = client
        .query("SELECT $1::TEXT", &[&"hello world"])
        .await?;

    // And then check that we got back the same string we sent over.
    let value: &str = rows[0].get(0);
    println!("value: {:?}", &value);
    assert_eq!(value, "hello world");

    Ok(())
}

#[tokio::main]
async fn main() {
    dotenv::dotenv().ok();
    let _ = db_main().await;
}

但是,我想像下面的主要内容一样自定义 tokio 运行时构建器——而不是使用 tokio 宏。 然而,当涉及到运行时的“tokio_postgres::connect”时,恐慌会说“没有反应器正在运行”。 我应该如何重新配置​​以下代码以在 tokio_postgres 中使用我自己的 Tokio 运行时实例(假设这甚至是我需要的)?

示例/nomacro.rs

use tokio_postgres::{NoTls, Error};

async fn db_main()-> Result<(), Error> {
    // Connect to the database.
    let conn_string = std::env::var("PG_CONNECT").unwrap();
    
    // This line panics with: 
    // "thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime', /Users/me/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/io/driver/mod.rs:254:13"
    let (client, connection) = tokio_postgres::connect(&conn_string, NoTls).await?;
    
    // ...
    Ok(())
}
    
fn main() {
    dotenv::dotenv().ok();

    // Tokio 0.3
    let rt:Runtime = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(6)
        .thread_name("my thread")
        .build()
        .unwrap();

    rt.block_on( async {
        db_main().await;
    });
}

我应该将运行时的引用传递给db_main()以提供 tokio_postgres Config的实例吗? 我已经尝试在 Cargo.toml 中使用"tokio-postgres = { version = "0.6.0", default-features = false}"禁用 tokio_postgres 中的 tokio 实例。

基线货物.toml:

[dependencies]
dotenv = "0.15.0"
tokio = { version = "0.3", features = ["rt-multi-thread", "macros"] }
tokio-postgres = { version = "0.6.0"}
# tokio-postgres = { version = "0.6.0", default-features = false}

非常多的学习(rust,tokio,postgres),我被这个问题提示到enable_io() ,我一时兴起尝试了一个可行的解决方案:

let rt = tokio::runtime::Builder::new_multi_thread()
    .worker_threads(6)
    .thread_name("my thread")
    .enable_io()
    .build()
    .unwrap();

我很乐意服从那些在东京更聪明的人。 这可能是一个不断发展的文档的情况。 恐慌源于这里在东京。 虽然 Postgres 需要“io”这一点很直观,但Tokio Builder的示例、 tokio_postgres和下面的代码并未暗示需要 Tokio builder 上的enable_io()才能使 tokio_postgres 工作。 如果我没有质疑这是一种完全错误的方法,我会提出一个文档拉取请求。

来自tokio/src/io/driver/mod.rs

cfg_rt! {
impl Handle {
    /// Returns a handle to the current reactor
    ///
    /// # Panics
    ///
    /// This function panics if there is no current reactor set and `rt` feature
    /// flag is not enabled.
    pub(super) fn current() -> Self {
        crate::runtime::context::io_handle()
            .expect("there is no reactor running, must be called from the context of Tokio runtime")
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM