我有一个项目需要使用 postgres 同步驱动程序(第三方库需要同步回调),它公开了一个 GRPC API。
不幸的是,rust-postgres 做了一个block_on
嵌套的#[tokio::main]
最小的复制将是:
use postgres::{Client, NoTls};
#[tokio::main]
async fn main() {
let _client = Client::connect("host=localhost dbname=template1 user=postgres", NoTls).unwrap();
}
与依赖项:
tokio = { version = "1.6.1", features = ["rt-multi-thread", "macros"] }
postgres = "0.19.1"
我得到错误:
'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.'
SQL 查询的性能并不重要,我可以很好地阻止它们。我宁愿不切换到,tokio-postgres
因为这意味着将所有内容重新包装为同步。