rust-tokio

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

≯℡__Kan透↙ 提交于 2021-02-02 09:53:08
问题 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 =

Borrowed value does not live long enough with a Tokio future

∥☆過路亽.° 提交于 2021-01-29 12:53:30
问题 I'm trying to write a simple HTTP server using Rust and tokio. Everything works fine until I want to send the response. The code is the following: use std::fs; use std::sync::Arc; use tokio::net::TcpListener; // 0.1.15 use tokio::prelude::*; fn main() { let addr = "0.0.0.0:8080".parse().unwrap(); let listener = TcpListener::bind(&addr).expect("unable to bind TCP listener"); let incoming = listener.incoming(); let server = incoming .map_err(|e| eprintln!("accept failed = {:?}", e)) .for_each(

Borrowed value does not live long enough with a Tokio future

[亡魂溺海] 提交于 2021-01-29 12:13:01
问题 I'm trying to write a simple HTTP server using Rust and tokio. Everything works fine until I want to send the response. The code is the following: use std::fs; use std::sync::Arc; use tokio::net::TcpListener; // 0.1.15 use tokio::prelude::*; fn main() { let addr = "0.0.0.0:8080".parse().unwrap(); let listener = TcpListener::bind(&addr).expect("unable to bind TCP listener"); let incoming = listener.incoming(); let server = incoming .map_err(|e| eprintln!("accept failed = {:?}", e)) .for_each(

“cannot recursively call into `Core`” when trying to achieve nested concurrency using Tokio

痞子三分冷 提交于 2021-01-28 04:12:09
问题 I'm building a service that periodically makes an HTTP request. I'm using tokio::timer::Delay as a periodic trigger and hyper to make the HTTP call. Using them together gives me the following error: thread 'tokio-runtime-worker-1' panicked at 'cannot recursively call into `Core`', libcore/option.rs:960:5 How can I make this work? Below is a simplified version of the service. main.rs extern crate futures; extern crate hyper; extern crate tokio; extern crate tokio_core; extern crate tokio_timer

Writing a chunk stream to a file asynchronously using hyper

拥有回忆 提交于 2021-01-27 13:01:48
问题 I am trying to create a simple function that downloads a remote file to a local filepath using hyper. I need the file write to be asynchronous as well (in my case I am using tokio_fs for that). Here is the code: View in the playground // Parts of the code were omitted, see the playground for full source code pub fn download_file( uri: Uri, file_location: &Path, ) -> Box<Future<Item = (), Error = DownloadFileError>> { let temp_dir_path = tempfile::tempdir().unwrap().into_path(); let file_name

Issue passing mutable Arc reference to hyper service_fn handler

蹲街弑〆低调 提交于 2021-01-24 07:09:21
问题 I've been trying the following Relevant imports and code shown use std::sync::{Arc, Mutex}; use std::thread; use hyper::rt::{self, Future, Stream}; use hyper::service::service_fn; use hyper::{Body, Request, Response, Server, StatusCode}; pub struct ChallengeState; pub struct ChallengeResponse; type BoxFut<'a> = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send + 'a>; fn handle_challengeproof<'a>( req: Request<Body>, challenge: &Arc<Mutex<ChallengeState>>, ) -> BoxFut<'a> { let

How to run an asynchronous task from a non-main thread in Tokio?

随声附和 提交于 2021-01-22 03:56:24
问题 use std::thread; use tokio::task; // 0.3.4 #[tokio::main] async fn main() { thread::spawn(|| { task::spawn(async { println!("123"); }); }) .join(); } When compiling I get a warning: warning: unused `std::result::Result` that must be used --> src/main.rs:6:5 | 6 | / thread::spawn(|| { 7 | | task::spawn(async { 8 | | println!("123"); 9 | | }); 10 | | }) 11 | | .join(); | |____________^ | = note: `#[warn(unused_must_use)]` on by default = note: this `Result` may be an `Err` variant, which should

How to run an asynchronous task from a non-main thread in Tokio?

你。 提交于 2021-01-22 03:53:29
问题 use std::thread; use tokio::task; // 0.3.4 #[tokio::main] async fn main() { thread::spawn(|| { task::spawn(async { println!("123"); }); }) .join(); } When compiling I get a warning: warning: unused `std::result::Result` that must be used --> src/main.rs:6:5 | 6 | / thread::spawn(|| { 7 | | task::spawn(async { 8 | | println!("123"); 9 | | }); 10 | | }) 11 | | .join(); | |____________^ | = note: `#[warn(unused_must_use)]` on by default = note: this `Result` may be an `Err` variant, which should

How to run an asynchronous task from a non-main thread in Tokio?

我只是一个虾纸丫 提交于 2021-01-22 03:52:42
问题 use std::thread; use tokio::task; // 0.3.4 #[tokio::main] async fn main() { thread::spawn(|| { task::spawn(async { println!("123"); }); }) .join(); } When compiling I get a warning: warning: unused `std::result::Result` that must be used --> src/main.rs:6:5 | 6 | / thread::spawn(|| { 7 | | task::spawn(async { 8 | | println!("123"); 9 | | }); 10 | | }) 11 | | .join(); | |____________^ | = note: `#[warn(unused_must_use)]` on by default = note: this `Result` may be an `Err` variant, which should

How to remotely shut down running tasks with Tokio

回眸只為那壹抹淺笑 提交于 2021-01-03 08:29:46
问题 I have a UDP socket that is receiving data pub async fn start() -> Result<(), std::io::Error> { loop { let mut data = vec![0; 1024]; socket.recv_from(&mut data).await?; } } This code is currently blocked on the .await when there is no data coming in. I want to gracefully shut down my server from my main thread, so how do I send a signal to this .await that it should stop sleeping and shut down instead? 回答1: Note: This answer currently links to the 1.x version of Tokio, but applies to Tokio 0