rust-tokio

How to set timeout for HTTP request with hyper, tokio and futures in Rust?

不羁的心 提交于 2019-12-19 03:45:14
问题 How do I set a timeout for HTTP request using asynchronous Hyper (>= 0.11)? Here is the example of the code without timeout: extern crate hyper; extern crate tokio_core; extern crate futures; use futures::Future; use hyper::Client; use tokio_core::reactor::Core; fn main() { let mut core = Core::new().unwrap(); let client = Client::new(&core.handle()); let uri = "http://stackoverflow.com".parse().unwrap(); let work = client.get(uri).map(|res| { res.status() }); match core.run(work) { Ok(status

How can I stop reading from a tokio::io::lines stream?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 09:01:07
问题 I want to terminate reading from a tokio::io::lines stream. I merged it with a oneshot future and terminated it, but tokio::run was still working. use futures::{sync::oneshot, *}; // 0.1.27 use std::{io::BufReader, time::Duration}; use tokio::prelude::*; // 0.1.21 fn main() { let (tx, rx) = oneshot::channel::<()>(); let lines = tokio::io::lines(BufReader::new(tokio::io::stdin())); let lines = lines.for_each(|item| { println!("> {:?}", item); Ok(()) }); std::thread::spawn(move || { std::thread

Is there any way to shutdown `tokio::runtime::current_thread::Runtime`?

回眸只為那壹抹淺笑 提交于 2019-12-13 17:48:19
问题 I'm using tokio::runtime::current_thread::Runtime and I want to able to run a future and stop the reactor in the same thread. The example on the page doesn't show how to stop the runtime. Is there any way I can do that? 回答1: The runtime will automatically shut down when when the future is complete if you use block_on : use std::time::{Duration, Instant}; use tokio::{prelude::*, runtime::current_thread, timer::Delay}; // 0.1.15 fn main() { let mut runtime = current_thread::Runtime::new()

The trait bound `futures::Future<Item=Arc<T>, Error=Box<Error + Send>>: Send` is not satisfied

。_饼干妹妹 提交于 2019-12-13 07:33:21
问题 I have the following (simplified) code snippet and I'm trying to run a future on a CpuPool: use futures::{self, Future, IntoFuture}; use std::error; use futures_cpupool::CpuPool; pub struct Store<T: 'static + Send + Sync> { inner: Arc<StoreInner<T, C, SerDe, P>>, } struct StoreInner<T: 'static + Send + Sync> { read_thread_pool: CpuPool, } impl<T: 'static + Send + Sync> Store<T> { pub fn get(self, obj_id: String) -> Box<Future<Item = Arc<T>, Error = Box<error::Error + Send>>> where T: for<'de>

tokio with multiqueue sometimes hangs, sometimes works

穿精又带淫゛_ 提交于 2019-12-13 06:28:37
问题 I'm trying to benchmark the crate multiqueue with tokio to implement something along the lines of publisher/subscriber by making Stream s that can be iterated. I'm not convinced on the efficiency (I may need dozens or hundreds of listeners which filter on the items and the single publisher will be publishing somewhere around 10 messages per millisecond), so I'd like to benchmark the approach before I commit to it. However, right now, I'm encountering a strange bug where sometimes the tokio:

How can I test a future that is bound to a tokio TcpStream?

我的梦境 提交于 2019-12-12 14:11:39
问题 I have a future which wraps a TCP stream in a Framed using the LinesCodec . When I try to wrap this in a test, I get the future blocking around 20% of the time, but because I have nothing listening on the socket I'm trying to connect to, I expect to always get the error: thread 'tokio-runtime-worker-0' panicked at 'error: Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }', src/lib.rs:35:24 note: Run with 'RUST_BACKTRACE=1' for a backtrace. This is the test code I have

How can I read from a tokio TCP connection without using the tokio_proto crate?

≯℡__Kan透↙ 提交于 2019-12-11 05:12:32
问题 I'm trying to write a TCP client to print incoming messages. I came up with the following code: extern crate bytes; extern crate futures; extern crate tokio_core; extern crate tokio_io; use futures::Future; use tokio_core::net::TcpStream; use tokio_core::reactor::Core; use tokio_io::AsyncRead; use bytes::BytesMut; fn main() { let mut core = Core::new().unwrap(); let handle = core.handle(); let connection = TcpStream::connect(&"127.0.0.1:8081".parse().unwrap(), &handle); let server =

How do I add tasks to a Tokio event loop that is running on another thread?

*爱你&永不变心* 提交于 2019-12-11 02:44:34
问题 I'd like to spin up a Tokio event loop alongside a Rocket server, then add events to this loop later on. I read Is there a way to launch a tokio::Delay on a new thread to allow the main loop to continue?, but it's still not clear to me how to achieve my goal. 回答1: As the documentation states: While current_thread::Runtime does not implement Send and cannot safely be moved to other threads, it provides a Handle that can be sent to other threads and allows to spawn new tasks from there. Here is

How to asynchronously explore a directory and its sub-directories?

烂漫一生 提交于 2019-12-11 01:29:41
问题 I need to explore a directory and all its sub-directories. I can explore the directory easily with recursion in a synchronous way: use failure::Error; use std::fs; use std::path::Path; fn main() -> Result<(), Error> { visit(Path::new(".")) } fn visit(path: &Path) -> Result<(), Error> { for e in fs::read_dir(path)? { let e = e?; let path = e.path(); if path.is_dir() { visit(&path)?; } else if path.is_file() { println!("File: {:?}", path); } } Ok(()) } When I try to do the same in an

How do I gracefully shutdown the Tokio runtime in response to a SIGTERM?

青春壹個敷衍的年華 提交于 2019-12-10 14:40:06
问题 I have a main function, where I create a Tokio runtime and run two futures on it. use tokio; fn main() { let mut runtime = tokio::runtime::Runtime::new().unwrap(); runtime.spawn(MyMegaFutureNumberOne {}); runtime.spawn(MyMegaFutureNumberTwo {}); // Some code to 'join' them after receiving an OS signal } How do I receive a SIGTERM , wait for all unfinished tasks ( NotReady s) and exit the application? 回答1: Dealing with signals is tricky and it would be too broad to explain how to handle all