From 9707981ec363e11f69ee45f883021558c4867836 Mon Sep 17 00:00:00 2001 From: Hsiang-Cheng Yang Date: Tue, 20 Aug 2024 04:16:58 +0800 Subject: [PATCH] Update async-sentiment.rs (#337) * Update async-sentiment.rs use `tokio::task::spawn_blocking` instead of `std::thread::spawn` * fmt --------- Co-authored-by: guillaume-be --- examples/async-sentiment.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/async-sentiment.rs b/examples/async-sentiment.rs index 9232773ae..f3342d1a0 100644 --- a/examples/async-sentiment.rs +++ b/examples/async-sentiment.rs @@ -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<()> { @@ -36,7 +36,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 +57,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?) } }