mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
Introduce the Tokio runtime: Reactor + Threadpool (#141)
This patch is an intial implementation of the Tokio runtime. The Tokio runtime provides an out of the box configuration for running I/O heavy asynchronous applications. As of now, the Tokio runtime is a combination of a work-stealing thread pool as well as a background reactor to drive I/O resources. This patch also includes tokio-executor, a hopefully short lived crate that is based on the futures 0.2 executor RFC. * Implement `Park` for `Reactor` This enables the reactor to be used as the thread parker for executors. This also adds an `Error` component to `Park`. With this change, a `Reactor` and a `CurrentThread` can be combined to achieve the capabilities of tokio-core.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
extern crate futures;
|
||||
extern crate tokio_threadpool;
|
||||
extern crate env_logger;
|
||||
|
||||
use tokio_threadpool::*;
|
||||
use futures::future::{self, Executor};
|
||||
|
||||
use std::sync::mpsc;
|
||||
|
||||
const ITER: usize = 2_000_000;
|
||||
// const ITER: usize = 30;
|
||||
|
||||
fn chained_spawn() {
|
||||
let pool = ThreadPool::new();
|
||||
let tx = pool.sender().clone();
|
||||
|
||||
fn spawn(tx: Sender, res_tx: mpsc::Sender<()>, n: usize) {
|
||||
if n == 0 {
|
||||
res_tx.send(()).unwrap();
|
||||
} else {
|
||||
let tx2 = tx.clone();
|
||||
tx.execute(future::lazy(move || {
|
||||
spawn(tx2, res_tx, n - 1);
|
||||
Ok(())
|
||||
})).ok().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||
let (res_tx, res_rx) = mpsc::channel();
|
||||
|
||||
for _ in 0..10 {
|
||||
spawn(tx.clone(), res_tx.clone(), ITER);
|
||||
}
|
||||
|
||||
for _ in 0..10 {
|
||||
res_rx.recv().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let _ = ::env_logger::init();
|
||||
chained_spawn();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
extern crate futures;
|
||||
extern crate tokio_threadpool;
|
||||
extern crate env_logger;
|
||||
|
||||
use tokio_threadpool::*;
|
||||
use futures::*;
|
||||
use futures::sync::oneshot;
|
||||
|
||||
pub fn main() {
|
||||
let _ = ::env_logger::init();
|
||||
|
||||
let pool = ThreadPool::new();
|
||||
let tx = pool.sender().clone();
|
||||
|
||||
let res = oneshot::spawn(future::lazy(|| {
|
||||
println!("Running on the pool");
|
||||
Ok::<_, ()>("complete")
|
||||
}), &tx);
|
||||
|
||||
println!("Result: {:?}", res.wait());
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
extern crate futures;
|
||||
extern crate tokio_threadpool;
|
||||
extern crate tokio_timer;
|
||||
extern crate env_logger;
|
||||
|
||||
use tokio_threadpool::*;
|
||||
use tokio_timer::Timer;
|
||||
|
||||
use futures::*;
|
||||
use futures::sync::oneshot::spawn;
|
||||
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn main() {
|
||||
let _ = ::env_logger::init();
|
||||
|
||||
let timer = Timer::default();
|
||||
{
|
||||
let pool = ThreadPool::new();
|
||||
let tx = pool.sender().clone();
|
||||
|
||||
let fut = timer.interval(Duration::from_millis(300))
|
||||
.for_each(|_| {
|
||||
println!("~~~~~ Hello ~~~");
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|_| unimplemented!());
|
||||
|
||||
spawn(fut, &tx).wait().unwrap();
|
||||
}
|
||||
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
Reference in New Issue
Block a user