From dc0416df30bdd7cdff6ce686fe4152f7732c11e2 Mon Sep 17 00:00:00 2001 From: Hsiang-Cheng Yang Date: Fri, 3 Mar 2023 14:37:44 +0800 Subject: [PATCH 1/2] Update async-sentiment.rs use `tokio::task::spawn_blocking` instead of `std::thread::spawn` --- examples/async-sentiment.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/examples/async-sentiment.rs b/examples/async-sentiment.rs index 9232773ae..32d56b8f0 100644 --- a/examples/async-sentiment.rs +++ b/examples/async-sentiment.rs @@ -1,11 +1,8 @@ -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<()> { @@ -36,7 +33,7 @@ impl SentimentClassifier { /// to interact with it pub fn spawn() -> (JoinHandle>, 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 }) } @@ -57,7 +54,7 @@ impl SentimentClassifier { /// Make the runner predict a sample and return the result pub async fn predict(&self, texts: Vec) -> Result> { let (sender, receiver) = oneshot::channel(); - task::block_in_place(|| self.sender.send((texts, sender)))?; + self.sender.send((texts, sender))?; Ok(receiver.await?) } } From 1666da87a818ff79b2e74f50c145fac12eda67ca Mon Sep 17 00:00:00 2001 From: Guillaume Becquin Date: Mon, 19 Aug 2024 20:32:36 +0100 Subject: [PATCH 2/2] fmt --- examples/async-sentiment.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/async-sentiment.rs b/examples/async-sentiment.rs index 32d56b8f0..f3342d1a0 100644 --- a/examples/async-sentiment.rs +++ b/examples/async-sentiment.rs @@ -2,7 +2,10 @@ use std::sync::mpsc; use anyhow::Result; use rust_bert::pipelines::sentiment::{Sentiment, SentimentConfig, SentimentModel}; -use tokio::{sync::oneshot, task::{self, JoinHandle}}; +use tokio::{ + sync::oneshot, + task::{self, JoinHandle}, +}; #[tokio::main] async fn main() -> Result<()> {