Skip to content

Commit

Permalink
Update async-sentiment.rs (#337)
Browse files Browse the repository at this point in the history
* Update async-sentiment.rs

use `tokio::task::spawn_blocking` instead of `std::thread::spawn`

* fmt

---------

Co-authored-by: guillaume-be <guillaume.becquin@gmail.com>
  • Loading branch information
rick68 and guillaume-be committed Aug 19, 2024
1 parent 9af98f8 commit 9707981
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions examples/async-sentiment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{
sync::mpsc,
thread::{self, JoinHandle},
};
use std::sync::mpsc;

use anyhow::Result;
use rust_bert::pipelines::sentiment::{Sentiment, SentimentConfig, SentimentModel};
use tokio::{sync::oneshot, task};
use tokio::{
sync::oneshot,
task::{self, JoinHandle},
};

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -36,7 +36,7 @@ impl SentimentClassifier {
/// to interact with it
pub fn spawn() -> (JoinHandle<Result<()>>, SentimentClassifier) {
let (sender, receiver) = mpsc::sync_channel(100);
let handle = thread::spawn(move || Self::runner(receiver));
let handle = task::spawn_blocking(move || Self::runner(receiver));
(handle, SentimentClassifier { sender })
}

Expand All @@ -57,7 +57,7 @@ impl SentimentClassifier {
/// Make the runner predict a sample and return the result
pub async fn predict(&self, texts: Vec<String>) -> Result<Vec<Sentiment>> {
let (sender, receiver) = oneshot::channel();
task::block_in_place(|| self.sender.send((texts, sender)))?;
self.sender.send((texts, sender))?;
Ok(receiver.await?)
}
}

0 comments on commit 9707981

Please sign in to comment.