rust-tokio

Getting multiple URLs concurrently with Hyper

妖精的绣舞 提交于 2019-12-06 07:01:49
问题 I am trying to adapt the Hyper basic client example to get multiple URLs concurrently. This is the code I currently have: extern crate futures; extern crate hyper; extern crate tokio_core; use std::io::{self, Write}; use std::iter; use futures::{Future, Stream}; use hyper::Client; use tokio_core::reactor::Core; fn get_url() { let mut core = Core::new().unwrap(); let client = Client::new(&core.handle()); let uris: Vec<_> = iter::repeat("http://httpbin.org/ip".parse().unwrap()).take(50).collect

How to select between a future and stream in Rust?

你离开我真会死。 提交于 2019-12-05 11:59:56
I've just started experimenting with futures/tokio in Rust. I can do really basic things with just futures or just with streams. I was wondering how you can select between future and a stream. How can I extend the toy problem from the tokio documentation to use tokio_timer::Timer to do a timed HTTPS request? extern crate futures; extern crate native_tls; extern crate tokio_core; extern crate tokio_io; extern crate tokio_tls; use std::io; use std::net::ToSocketAddrs; use futures::Future; use native_tls::TlsConnector; use tokio_core::net::TcpStream; use tokio_core::reactor::Core; use tokio_tls:

Why does calling tokio::spawn result in the panic “SpawnError { is_shutdown: true }”?

此生再无相见时 提交于 2019-12-05 11:09:43
I want to use Delay to do some work later. If I use tokio::run , it just works fine, but it panics when using tokio::spawn : use std::sync::mpsc; use std::time::*; use tokio::prelude::*; // 0.1.14 fn main() { let (tx, rx) = mpsc::channel(); let task = tokio::timer::Delay::new(Instant::now() + Duration::from_secs(1)) .map(move |_| { tx.send(String::from("hello")).unwrap(); () }) .map_err(|e| { panic!("{:?}", e); }); tokio::spawn(task); let msg = rx.recv().unwrap(); println!("{}", msg); } thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SpawnError { is_shutdown: true }',

The trait bound `(): futures::Future` is not satisfied when using TcpConnectionNew

依然范特西╮ 提交于 2019-12-05 03:09:08
I am trying to write a simple TCP client in Rust using Tokio crate. My code is pretty close to this example minus the TLS: 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::io; fn main() { let mut core = Core::new().unwrap(); let handle = core.handle(); let connection = TcpStream::connect(&"127.0.0.1:8080".parse().unwrap(), &handle); let server = connection.and_then(|stream| { io::write_all(stream, b"hello"); }); core.run(server).unwrap(); } However, compilation fails with the

Getting multiple URLs concurrently with Hyper

前提是你 提交于 2019-12-04 12:18:08
I am trying to adapt the Hyper basic client example to get multiple URLs concurrently. This is the code I currently have: extern crate futures; extern crate hyper; extern crate tokio_core; use std::io::{self, Write}; use std::iter; use futures::{Future, Stream}; use hyper::Client; use tokio_core::reactor::Core; fn get_url() { let mut core = Core::new().unwrap(); let client = Client::new(&core.handle()); let uris: Vec<_> = iter::repeat("http://httpbin.org/ip".parse().unwrap()).take(50).collect(); for uri in uris { let work = client.get(uri).and_then(|res| { println!("Response: {}", res.status()

How do I use async/await syntax with Tokio?

删除回忆录丶 提交于 2019-12-04 01:33:10
问题 I'm trying to use async/await with processes in Rust. I'm using tokio and tokio-process : #![feature(await_macro, async_await, futures_api)] extern crate tokio; extern crate tokio_process; use std::process::Command; use tokio_process::CommandExt; fn main() { tokio::run_async(main_async()); } async fn main_async() { let out = Command::new("echo") .arg("hello") .arg("world") .output_async(); let s = await!(out); } Here is the error that I get: error[E0277]: the trait bound `tokio_process:

Asynchronously reconnecting a client to a server in an infinite loop

旧时模样 提交于 2019-12-03 06:53:14
问题 I'm not able to create a client that tries to connect to a server and: if the server is down it has to try again in an infinite loop if the server is up and connection is successful, when the connection is lost (i.e. server disconnects the client) the client has to restart the infinite loop to try to connect to the server Here's the code to connect to a server; currently when the connection is lost the program exits. I'm not sure what the best way to implement it is; maybe I have to create a

Asynchronously reconnecting a client to a server in an infinite loop

你离开我真会死。 提交于 2019-12-02 20:31:54
I'm not able to create a client that tries to connect to a server and: if the server is down it has to try again in an infinite loop if the server is up and connection is successful, when the connection is lost (i.e. server disconnects the client) the client has to restart the infinite loop to try to connect to the server Here's the code to connect to a server; currently when the connection is lost the program exits. I'm not sure what the best way to implement it is; maybe I have to create a Future with an infinite loop? extern crate tokio_line; use tokio_line::LineCodec; fn get_connection

How do I solve “the trait bound `[closure]: tokio::prelude::Future` is not satisfied” when calling tokio::spawn?

感情迁移 提交于 2019-12-02 11:25:16
extern crate tokio; // 0.1.22 use tokio::io; use tokio::net::TcpListener; use tokio::prelude::*; use bytes::Bytes; // 0.4.12 fn main() { let addr = "0.0.0.0:1502".parse().unwrap(); let mut listener = TcpListener::bind(&addr).unwrap(); let done = listener .incoming() .map_err(|e| println!("failed to accept socket; error = {:?}", e)) .for_each(move |socket| { let process = move || {}; tokio::spawn(process) }); tokio::run(done); Ok(()); tokio::run(done); } error[E0277]: the trait bound `[closure@src/main.rs:17:27: 17:37]: tokio::prelude::Future` is not satisfied --> src/main.rs:19:13 | 19 | tokio

How do I use async/await syntax with Tokio?

自作多情 提交于 2019-12-01 23:15:55
I'm trying to use async/await with processes in Rust. I'm using tokio and tokio-process : #![feature(await_macro, async_await, futures_api)] extern crate tokio; extern crate tokio_process; use std::process::Command; use tokio_process::CommandExt; fn main() { tokio::run_async(main_async()); } async fn main_async() { let out = Command::new("echo") .arg("hello") .arg("world") .output_async(); let s = await!(out); } Here is the error that I get: error[E0277]: the trait bound `tokio_process::OutputAsync: std::future::Future` is not satisfied --> src/main.rs:21:13 | 21 | let s = await!(out); | ^^^^^