我正在弹性搜索的博客文章中使用有关其新包装箱的示例代码,但无法使其按预期工作。出现线程恐慌thread 'main' panicked at 'not currently running on the Tokio runtime.'
。
什么是Tokio运行时,如何配置它,为什么必须?
use futures::executor::block_on;
async elastic_search_example() -> Result<(), Box<dyn Error>> {
let index_response = client
.index(IndexParts::IndexId("tweets", "1"))
.body(json!({
"user": "kimchy",
"post_date": "2009-11-15T00:00:00Z",
"message": "Trying out Elasticsearch, so far so good?"
}))
.refresh(Refresh::WaitFor)
.send()
.await?;
if !index_response.status_code().is_success() {
panic!("indexing document failed")
}
let index_response = client
.index(IndexParts::IndexId("tweets", "2"))
.body(json!({
"user": "forloop",
"post_date": "2020-01-08T00:00:00Z",
"message": "Indexing with the rust client, yeah!"
}))
.refresh(Refresh::WaitFor)
.send()
.await?;
if !index_response.status_code().is_success() {
panic!("indexing document failed")
}
}
fn main() {
block_on(elastic_search_example());
}
看来Elasticsearch的板条箱在内部使用Tokio,因此您也必须使用它来匹配他们的假设。
寻找block_on
他们的文档的功能,我有这个。因此,看来您main
应该看起来像这样:
use tokio::runtime::Runtime;
fn main() {
Runtime::new()
.expect("Failed to create Tokio runtime")
.block_on(elastic_search_example());
}
或者,您可以使自己main
本身与属性macro异步,这将生成运行时创建并block_on
为您调用:
#[tokio::main]
async fn main() {
elastic_search_example().await;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句