【发布时间】:2020-06-23 14:02:06
【问题描述】:
我使用rust_bert 来总结文本。我需要用rust_bert::pipelines::summarization::SummarizationModel::new 设置一个模型,它从互联网上获取模型。它使用tokio 异步执行此操作,(我认为)我遇到的问题是我在另一个 Tokio 运行时中运行 Tokio 运行时,如错误消息所示:
Downloading https://cdn.huggingface.co/facebook/bart-large-cnn/config.json to "/home/(censored)/.cache/.rustbert/bart-cnn/config.json"
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.', /home/(censored)/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.21/src/runtime/enter.rs:38:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
我尝试过运行模型同步获取
tokio::task::spawn_blocking
和tokio::task::block_in_place
但他们都不为我工作。 block_in_place 给出了与不存在相同的错误,而spawn_blocking 真的 似乎对我没有用处。
我也尝试过使summarize_text 异步,但这并没有太大帮助。 Github问题
tokio-rs/tokio#2194
和 Reddit 帖子
"'Cannot start a runtime from within a runtime.' with Actix-Web And Postgresql"
看起来很相似(相同的错误消息),但它们对找到解决方案没有多大帮助。
我遇到问题的代码如下:
use egg_mode::tweet;
use rust_bert::pipelines::summarization::SummarizationModel;
fn summarize_text(model: SummarizationModel, text: &str) -> String {
let output = model.summarize(&[text]);
// @TODO: output summarization
match output.is_empty() {
false => "FALSE".to_string(),
true => "TRUE".to_string(),
}
}
#[tokio::main]
async fn main() {
let model = SummarizationModel::new(Default::default()).unwrap();
let token = egg_mode::auth::Token::Bearer("obviously not my token".to_string());
let tweet_id = 1221552460768202756; // example tweet
println!("Loading tweet [{id}]", id = tweet_id);
let status = tweet::show(tweet_id, &token).await;
match status {
Err(err) => println!("Failed to fetch tweet: {}", err),
Ok(tweet) => {
println!(
"Original tweet:\n{orig}\n\nSummarized tweet:\n{sum}",
orig = tweet.text,
sum = summarize_text(model, &tweet.text)
);
}
}
}
【问题讨论】:
-
很难回答您的问题,因为它不包含minimal reproducible example。我们无法分辨代码中存在哪些 crate(及其版本)、类型、特征、字段等。如果您尝试在一个全新的 Cargo 项目中重现您的错误,我们会更轻松地为您提供帮助,然后 edit 您的问题将包含完整的示例。您可以使用Rust-specific MRE tips 来减少您在此处发布的原始代码。谢谢!
-
当问题是由相关板条箱“引起”时,MRE 是否也适用? (rust_bert)——不管怎样,我现在加一个。一瞬间。 (编辑:即可以在 mre 中使用 rust_bert 吗?)
-
可以在 mre 中使用 rust_bert — 是的,但是,就像我说的:箱子(及其版本)。该版本需要能够重现问题。
标签: rust rust-tokio