I am using rust diesel transaction to do some async task, now I am tried to use a new tokio runtime like this(this just a minimal demo to show the issue):
use diesel::result::Error;
use diesel::Connection;
use rust_wheel::config::db::config;
use tokio::runtime::Runtime;
#[tokio::main]
async fn main() {
let mut connection = config::connection("TEX_DATABASE_URL".to_string());
let _trans_result: Result<(), Error> = connection.transaction(|_connection| {
let rt = Runtime::new().unwrap();
Ok(rt.block_on(async { do_create_proj_trans().await }))
});
}
async fn do_create_proj_trans() {
println!("doing...")
}
该法典显示错误,如:
thread main panicked at Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks. , /Users/xiaoqiangjiang/.cargo/registry/src/mirrors.tuna.tsinghua.edu.cn-df7c3c540f42cdbd/tokio-1.32.0/src/runtime/scheduler/multi_thread/mod.rs:86:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
当我去掉起算时间时,就显示出这样的错误:
`await` is only allowed inside `async` functions and blocks
only allowed inside `async` functions and blocks
能否在ru式柴油机体内发挥合成功能? 货物:
[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"
[dependencies]
tokio = { version = "1.17.0", features = ["full"] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
futures = "0.3"
tokio-stream = "0.1"
rust_wheel = { git = "https://github.com/jiangxiaoqiang/rust_wheel.git", branch = "diesel2.0" }
log4rs = "1.2.0"
log = "0.4.0"
diesel = { version = "2.1.0", features = ["postgres","64-column-tables","chrono","serde_json"] }