在没有主宏的情况下,如何使用定制的tokio运行时构建器来实现这个tokio_postgres示例?
根据tokio_postgres docs,这可以正常工作
examples/withmacro.rs宏.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运行时实例(假设这就是我所需要的)?
examples/omacro.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的实例?我已经尝试在tokio_postgres中使用Cargo.toml中的"tokio-postgres = { version = "0.6.0", default-features = false}"禁用tokio实例。
基线Cargo.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}发布于 2020-11-04 05:12:55
学了很多(rust,tokio,postgres),我被this issue提示到enable_io(),我一时兴起尝试了一个有效的解决方案:
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(6)
.thread_name("my thread")
.enable_io()
.build()
.unwrap();我很乐意听从东京的智者的意见。这可能是一个不断发展的文档的案例。恐慌源于东京的here。虽然Postgres需要"io",但Tokio Builder、tokio_postgres的示例和下面的代码并不暗示需要Tokio构建器上的enable_io()才能使tokio_postgres工作。如果我没有质疑,这是一个完全错误的方法,我会提出一个文档拉取请求。
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")
}
}
}https://stackoverflow.com/questions/64658556
复制相似问题