rust-tokio

How can I create a Tokio runtime inside another Tokio runtime without getting the error “Cannot start a runtime from within a runtime”?

牧云@^-^@ 提交于 2021-02-15 04:50:22
问题 I'm using rust_bert for summarising text. I need to set a model with rust_bert::pipelines::summarization::SummarizationModel::new , which fetches the model from the internet. It does this asynchronously using tokio and the issue that (I think) I'm running into is that I am running the Tokio runtime within another Tokio runtime, as indicated by the error message: Downloading https://cdn.huggingface.co/facebook/bart-large-cnn/config.json to "/home/(censored)/.cache/.rustbert/bart-cnn/config

Why do I not get a wakeup for multiple futures when they use the same underlying socket?

梦想与她 提交于 2021-02-11 04:54:57
问题 I have code which sends data to multiple UDP endpoints using same local UdpSocket: use futures::stream::FuturesUnordered; use futures::StreamExt; use std::{ future::Future, net::{Ipv4Addr, SocketAddr}, pin::Pin, task::{Context, Poll}, }; use tokio::net::UdpSocket; #[tokio::main] async fn main() { let server_0: SocketAddr = (Ipv4Addr::UNSPECIFIED, 12000).into(); let server_2: SocketAddr = (Ipv4Addr::UNSPECIFIED, 12002).into(); let server_1: SocketAddr = (Ipv4Addr::UNSPECIFIED, 12001).into();

“blocking annotated I/O must be called from the context of the Tokio runtime” when reading stdin in async task

回眸只為那壹抹淺笑 提交于 2021-02-10 23:27:37
问题 I'm trying to read from stdin in an async task, spawned with tokio::spawn . The executor is crated as let mut executor = tokio::runtime::current_thread::Runtime::new().unwrap(); the main task is then run with executor.task(...) , which spawns other tasks with tokio::spawn() . fn main then calls executor.run().unwrap(); to wait for all tasks to finish. The problem is when I do let mut stdin = tokio::io::stdin(); let mut read_buf: [u8; 1024] = [0; 1024]; ... stdin.read(&mut read_buf).await I

Rust futures — adapting a function as a Sink

那年仲夏 提交于 2021-02-08 11:15:17
问题 I have something similar to the tokio connect example with a method that accepts a sink: pub async fn connect( addr: &SocketAddr, mut stdin: impl Stream<Item = Result<Request, io::Error>> + Unpin, mut stdout: impl Sink<Response, Error = io::Error> + Unpin, ) -> Result<(), Box<dyn Error>> { Is there a standard/easy way to adapt a function to a sink for printing and/or transformation? eg. something like: connect(.., .., sink::from_function(|r| match r { Ok(response) => println!("received a

Rust futures — adapting a function as a Sink

烈酒焚心 提交于 2021-02-08 11:11:59
问题 I have something similar to the tokio connect example with a method that accepts a sink: pub async fn connect( addr: &SocketAddr, mut stdin: impl Stream<Item = Result<Request, io::Error>> + Unpin, mut stdout: impl Sink<Response, Error = io::Error> + Unpin, ) -> Result<(), Box<dyn Error>> { Is there a standard/easy way to adapt a function to a sink for printing and/or transformation? eg. something like: connect(.., .., sink::from_function(|r| match r { Ok(response) => println!("received a

Return Future value from a function

北城以北 提交于 2021-02-05 10:30:38
问题 I recently started to learn Rust and I'm not sure how I can return future value from a function that should return a Result. When I try to return just the response variable and remove the Result output, I get an error: cannot use the ? operator in a function that returns std::string::String #[tokio::main] async fn download() -> Result<(),reqwest::Error> { let url = "https://query1.finance.yahoo.com/v8/finance/chart/TSLA"; let response = reqwest::get(url) .await? .text() .await?; Ok(response)

Return Future value from a function

帅比萌擦擦* 提交于 2021-02-05 10:25:10
问题 I recently started to learn Rust and I'm not sure how I can return future value from a function that should return a Result. When I try to return just the response variable and remove the Result output, I get an error: cannot use the ? operator in a function that returns std::string::String #[tokio::main] async fn download() -> Result<(),reqwest::Error> { let url = "https://query1.finance.yahoo.com/v8/finance/chart/TSLA"; let response = reqwest::get(url) .await? .text() .await?; Ok(response)

Why do I get the error “there is no reactor running, must be called from the context of Tokio runtime” even though I have #[tokio::main]?

為{幸葍}努か 提交于 2021-02-04 17:18:22
问题 I'm following the mdns Rust documentation and pasted the example code but it throws the following error: thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime' Here's the code that I have: use futures_util::{pin_mut, stream::StreamExt}; use mdns::{Error, Record, RecordKind}; use std::{net::IpAddr, time::Duration}; const SERVICE_NAME: &'static str = "_googlecast._tcp.local"; #[tokio::main] async fn main() -> Result<(), Error> { // Iterate

Spawning tasks with non-static lifetimes with tokio 0.1.x

纵然是瞬间 提交于 2021-02-04 07:08:50
问题 I have a tokio core whose main task is running a websocket (client). When I receive some messages from the server, I want to execute a new task that will update some data. Below is a minimal failing example: use tokio_core::reactor::{Core, Handle}; use futures::future::Future; use futures::future; struct Client { handle: Handle, data: usize, } impl Client { fn update_data(&mut self) { // spawn a new task that updates the data self.handle.spawn(future::ok(()).and_then(|x| { self.data += 1; //

(tokio::spawn) borrowed value does not live long enough — argument requires that `sleepy` is borrowed for `'static`

≡放荡痞女 提交于 2021-02-02 09:56:35
问题 This MWE shows the use of tokio::spawn in for in loop. The commented code sleepy_futures.push(sleepy.sleep_n(2)); works fine, but does not run/poll the async function. Basically, I would like to run a bunch of async functions at the same time. I am happy to change the implementation of Sleepy or use another library/technique. pub struct Sleepy; impl Sleepy { pub async fn sleep_n(self: &Self, n: u64) -> String { sleep(Duration::from_secs(n)); "test".to_string() } } #[tokio::main(core_threads =