- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Rust 的新手。我正在将 mongodb 与异步运行时(tokio)一起使用。
我想全局初始化 mongo 客户端,所以我使用了一个名为 lazy_static 的 crate .问题是mongodb是异步连接的,现在lazy_static不支持async
关键词...
use mongodb::Client;
async {
let client = Client::with_uri_str(&env_var("MONGO_URL")).await.unwrap();
}
那么我如何初始化
client
全局对象?
最佳答案
如果您使用新的运行时并且首先在现有运行时的上下文中使用惰性静态,如下例所示:
use lazy_static::lazy_static;
use mongodb::Client;
lazy_static! {
static ref CLIENT: Client = {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let uri = std::env::var("MONGO_URL").unwrap();
let client = Client::with_uri_str(&uri).await.unwrap();
client
})
};
}
#[tokio::main]
async fn main() {
let _db = CLIENT.database("local");
}
你会得到提到的错误:
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.', C:\Users\kmdreko\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.6.1\src\runtime\enter.rs:39:9
您可以通过使用不同的运行时(
futures
vs
tokio
vs
async-std
)来规避这一点,但这并不理想,因为它仍然会阻止底层运行。
main
中初始化它。 .这样你就可以直接使用异步运行时而不必担心在其他地方需要一个:
use mongodb::Client;
use once_cell::sync::OnceCell;
static CLIENT: OnceCell<Client> = OnceCell::new();
#[tokio::main]
async fn main() {
let uri = std::env::var("MONGO_URL").unwrap();
let client = Client::with_uri_str(&uri).await.unwrap();
CLIENT.set(client).unwrap();
let _db = CLIENT.get().unwrap().database("local");
}
这可以通过
OnceCell
完成(如上所示)或类似
RwLock
的东西如有必要。
use async_once::AsyncOnce;
use lazy_static::lazy_static;
use mongodb::Client;
lazy_static! {
static ref CLIENT: AsyncOnce<Client> = AsyncOnce::new(async {
let uri = std::env::var("MONGO_URL").unwrap();
let client = Client::with_uri_str(&uri).await.unwrap();
client
});
}
#[tokio::main]
async fn main() {
let _db = CLIENT.get().await.database("local");
}
这假设客户端的所有或几乎所有使用都在异步上下文中。
关于asynchronous - Rust lazy_static 与 async/await?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67650879/
我使用 lazy_static 在内存中保存一个 HashMap。我使用两种方法添加和获取元素,但我在生命周期方面遇到了一些问题。 这是我的代码: #[macro_use] extern crate
我正在尝试使用递归公用表表达式(大约80行SELECT语句)集成一个相当复杂的SQL查询。有两个不同的查询可以为递归设置种子。我不想在我的代码中嵌入两个不同的80行SQL语句,它们之间只有一行不同,谢
#[derive(Serialize)] pub struct SLPInfoVersion { name: String, protocol: i32 } impl SLPInfoV
我尝试创建一个 HashMap 并将函数作为值: #[macro_use] extern crate lazy_static; use std::collections::HashMap; lazy_
我有一个大项目,我在其中使用 lazy_static 创建一个 singleton。我认为 lazy_static crate 中存在错误(仅出现在大型项目中)或者我做错了什么,因为必须调用一次以创建
如果我取消注释 create_log,log 和 LOG 都会打印在控制台上。没有它,什么也不会打印。这是怎么回事? #[macro_use] extern crate slog; extern cr
我是 Rust 的新手。我正在将 mongodb 与异步运行时(tokio)一起使用。 我想全局初始化 mongo 客户端,所以我使用了一个名为 lazy_static 的 crate .问题是mon
我正在尝试使用 lazy_static crate 初始化一些静态变量,这些变量通过读取 build.rs 中的一些环境变量来赋值。我想要实现的类似于 this post . 我的代码如下: lazy
我想将一些 json 读入静态 HashMap ,并且正在使用 lazy_static和 serde ,但我不知道如何(如果有的话)解决这个 serde终身问题: #[macro_use] exter
这个问题在这里已经有了答案: Why does a lazy-static value claim to not implement a trait that it clearly implemen
我想分享一个 evmap ,一个无锁的、最终一致的、并发的多值映射,跨 Rust 程序中的所有线程。 天真地,它看起来像这样: #[macro_use] extern crate lazy_stati
这个问题在这里已经有了答案: Trying to return reference from RwLock, "borrowed value does not live long enough" E
我正在使用模拟函数编写测试,使用 Mutex 控制测试之间的返回值: #[macro_use] extern crate lazy_static; #[cfg(test)] pub use mock:
这段代码: #[macro_use] extern crate lazy_static; extern crate mysql; use mysql::*; fn some_fn() { la
我正在使用 Rust,为了方便起见,我想使用一个全局可变的 HashMap。然而,虽然可以使用 lazy_static 和 Mutex 定义一个全局的、可变的 HashMap,但是对于我的 Strin
我是一名优秀的程序员,十分优秀!