Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make downloads asyncronous #42

Closed
Veetaha opened this issue May 24, 2020 · 3 comments
Closed

Make downloads asyncronous #42

Veetaha opened this issue May 24, 2020 · 3 comments

Comments

@Veetaha
Copy link

Veetaha commented May 24, 2020

I failed to integrate this library into my application which natively uses async/await and tokio.
I simply cannot create a model, because I get

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 

every time I try to create SentimentModel::new().
I may be a newbie in tokio, so I tried

let model = tokio::task::block_in_place(|| SentimentModel::new(Default::default())).unwrap();

But this didn't solve the problem.
Also doing this:

let model = tokio::task::spawn_blocking(|| SentimentModel::new(Default::default()).unwrap())
    .await.unwrap();

also doesn't work because Rust complaints about sending some internal std::rc::Rc between threads. Even wrapping it all with Arc<Mutex<SentimentModel>> doesn't help.
I am simply perplexed and don't know how to use this crate inside of async fns...

@guillaume-be
Copy link
Owner

Hello,

The dependencies download is asynchronous. The code is located at:

pub async fn download_resource(resource: &Resource) -> failure::Fallible<&PathBuf> {

I believe the problem is that I declared a #[tokio::main] which would not allow you to do so in your main application.

I am pushing some changes to make the overall function synchronous and use a local runtime instead. As I do not have code available to reproduce the issue, could you please check-out the branch async_download and let me know if this solves the issue?

@Veetaha
Copy link
Author

Veetaha commented May 24, 2020

Sorry for not mentioning, basically the reproduction is:

use rust_bert::pipelines::sentiment::SentimentModel;

#[tokio::main]
async fn main() {
    let model = SentimentModel::new(Default::default()).unwrap();

    // or
    let model = tokio::task::block_in_place(|| SentimentModel::new(Default::default())).unwrap();

    // or the following which you cannot do due to rust complaining about sending Rc between threads
    let model = tokio::task::spawn_blocking(|| SentimentModel::new(Default::default()).unwrap())
        .await.unwrap();
}

It should produce the Cannot start a runtime from within a runtime, let me know if it doesn't!

@guillaume-be
Copy link
Owner

Hello,

The library is by design currently synchronous, because the download part of it is a minimal feature.
To run the code in an asynchronous environment, please spawn a blocking thread:

#[tokio::main]
async fn main() {
    task::spawn_blocking(move || { let model = SentimentModel::new(Default::default()).unwrap(); });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants