rust-tokio

Initializing a Rust variable passed to async code such as tokio and hyper

故事扮演 提交于 2020-02-16 05:29:06
问题 I have a value that cannot be computed at compile time. It needs to be computed before any of the app code runs, and then it will only be read throughout the lifetime of the app. It also needs to be passed around to executors such as tokio and hyper handlers. How can I create such a value safely, idiomatically and without unneeded performance losses? If I create it in main and pass it to hyper , it does not live long enough. If I create it with lazy_static! , it only gets computed when it's

How to convert a Future into a Stream?

蹲街弑〆低调 提交于 2020-02-06 08:02:10
问题 I'm trying to use async_std to receive UDP datagrams from the network. There is a UdpSocket that implements async recv_from, this method returns a future but I need a async_std::stream::Stream that gives a stream of UDP datagrams because it is a better abstraction. I've found tokio::net::UdpFramed that does exactly what I need but it is not available in current versions of tokio. Generally speaking the question is how do I convert Future s from a given async function into a Stream ? 回答1: For

Unable to tokio::run a boxed Future because the trait bound Send is not satisfied [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-01-25 08:27:45
问题 This question already has answers here : The trait bound `futures::Future<Item=Arc<T>, Error=Box<Error + Send>>: Send` is not satisfied (1 answer) Sending trait objects between threads in Rust (1 answer) Closed 2 years ago . I have a function that should optionally run a future or do nothing, depending on parameters. I tried putting a Box around the two futures that will be returned, a tokio::prelude::future::Done<Item=(), Error=()> that immediately resolves to Ok(()) , and a tokio::timer:

Is there any way to create a async stream generator that yields the result of repeatedly calling a function?

懵懂的女人 提交于 2020-01-22 02:21:25
问题 I want to build a program that collects weather updates and represents them as a stream. I want to call get_weather() in an infinite loop, with 60 seconds delay between finish and start . A simplified version would look like this: async fn get_weather() -> Weather { /* ... */ } fn get_weather_stream() -> impl futures::Stream<Item = Weather> { loop { tokio::timer::delay_for(std::time::Duration::from_secs(60)).await; let weather = get_weather().await; yield weather; // This is not supported //

How can I perform parallel asynchronous HTTP GET requests with reqwest?

☆樱花仙子☆ 提交于 2020-01-10 02:04:12
问题 The async example is useful, but being new to Rust and Tokio, I am struggling to work out how to do N requests at once, using URLs from a vector, and creating an iterator of the response HTML for each URL as a string. How could this be done? 回答1: As of reqwest 0.10: use futures::{stream, StreamExt}; // 0.3.1 use reqwest::Client; // 0.10.0 use tokio; // 0.2.4, features = ["macros"] const PARALLEL_REQUESTS: usize = 2; #[tokio::main] async fn main() { let client = Client::new(); let urls = vec![

Is there a way to launch a tokio::Delay on a new thread to allow the main loop to continue?

霸气de小男生 提交于 2020-01-06 05:50:10
问题 I am trying to run a function at the end of a delay if the timer is not canceled. The use case is a press and hold / double tap for user input. The main issue that I am having is that the tokio::run(task); stops the main loop from executing thus prevents me from evaluating the state of the users control. fn start_timer(&self) { let function_name = self.combo.function_name.clone(); let when = Instant::now() + Duration::from_millis(self.combo.timer_value as u64); let task = Delay::new(when)

The example from the “chaining computations” section of the Tokio docs does not compile: “expected struct `std::io::Error`, found ()”

守給你的承諾、 提交于 2020-01-05 09:34:42
问题 I am learning Tokio. I read Getting asynchronous from the official docs, but the source code from the Chaining computations section cannot be compiled under the latest Rust version (Rust 2018, v1.31): extern crate tokio; extern crate bytes; #[macro_use] extern crate futures; use tokio::io::AsyncWrite; use tokio::net::{TcpStream, tcp::ConnectFuture}; use bytes::{Bytes, Buf}; use futures::{Future, Async, Poll}; use std::io::{self, Cursor}; // HelloWorld has two states, namely waiting to connect

How to get tokio-io's async_read for a File handle

大城市里の小女人 提交于 2020-01-04 01:53:09
问题 I want to stream lines out of a file handle and I do not know how to satisfy the trait bound that a File has async_read : use std::fs::File; use std::io::{ BufReader, BufRead }; use tokio_core::reactor::Handle; use tokio_io::io::lines; use tokio_io::AsyncRead; struct DockerLog { path: String } impl DockerLog { pub fn new(path: String) -> DockerLog { DockerLog { path: path } } pub fn read_lines(&self, handle: &Handle) { let file : File = File::open(&self.path[..]).unwrap(); let l = lines

How do I read the entire body of a Tokio-based Hyper request?

谁说我不能喝 提交于 2019-12-23 21:04:44
问题 I want to write a server using the current master branch of Hyper that saves a message that is delivered by a POST request and sends this message to every incoming GET request. I have this, mostly copied from the Hyper examples directory: extern crate futures; extern crate hyper; extern crate pretty_env_logger; use futures::future::FutureResult; use hyper::{Get, Post, StatusCode}; use hyper::header::{ContentLength}; use hyper::server::{Http, Service, Request, Response}; use futures::Stream;

HTTP request inside actix-web handler -> Multiple executors at once: EnterError

狂风中的少年 提交于 2019-12-23 21:02:59
问题 When creating a hyper post request inside an actix-web resolver, the following error is thrown - how can one send one a http request by spawning the request into the existing executor? thread 'actix-rt:worker:1' panicked at 'Multiple executors at once: EnterError { reason: "attempted to run an executor while another executor is already running" }', src/libcore/result.rs:999:5 note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace. Panic in Arbiter thread, shutting down