-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add futures executor threadpool (#3198)
- Loading branch information
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// This example shows how to use the tokio runtime with any other executor | ||
// | ||
//It takes advantage from RuntimeExt which provides the extension to customize your | ||
//runtime. | ||
|
||
use tokio::net::TcpListener; | ||
use tokio::runtime::Builder; | ||
use tokio::sync::oneshot; | ||
use tokio_util::context::RuntimeExt; | ||
|
||
fn main() { | ||
let (tx, rx) = oneshot::channel(); | ||
let rt1 = Builder::new_multi_thread() | ||
.worker_threads(1) | ||
// no timer! | ||
.build() | ||
.unwrap(); | ||
let rt2 = Builder::new_multi_thread() | ||
.worker_threads(1) | ||
.enable_all() | ||
.build() | ||
.unwrap(); | ||
|
||
// Without the `HandleExt.wrap()` there would be a panic because there is | ||
// no timer running, since it would be referencing runtime r1. | ||
let _ = rt1.block_on(rt2.wrap(async move { | ||
let listener = TcpListener::bind("0.0.0.0:0").await.unwrap(); | ||
println!("addr: {:?}", listener.local_addr()); | ||
tx.send(()).unwrap(); | ||
})); | ||
futures::executor::block_on(rx).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This example shows how to use the tokio runtime with any other executor | ||
// | ||
// The main components are a spawn fn that will wrap futures in a special future | ||
// that will always enter the tokio context on poll. This only spawns one extra thread | ||
// to manage and run the tokio drivers in the background. | ||
|
||
use tokio::net::TcpListener; | ||
use tokio::sync::oneshot; | ||
|
||
fn main() { | ||
let (tx, rx) = oneshot::channel(); | ||
|
||
my_custom_runtime::spawn(async move { | ||
let listener = TcpListener::bind("0.0.0.0:0").await.unwrap(); | ||
|
||
println!("addr: {:?}", listener.local_addr()); | ||
|
||
tx.send(()).unwrap(); | ||
}); | ||
|
||
futures::executor::block_on(rx).unwrap(); | ||
} | ||
|
||
mod my_custom_runtime { | ||
use once_cell::sync::Lazy; | ||
use std::future::Future; | ||
use tokio_util::context::TokioContext; | ||
|
||
pub fn spawn(f: impl Future<Output = ()> + Send + 'static) { | ||
EXECUTOR.spawn(f); | ||
} | ||
|
||
struct ThreadPool { | ||
inner: futures::executor::ThreadPool, | ||
rt: tokio::runtime::Runtime, | ||
} | ||
|
||
static EXECUTOR: Lazy<ThreadPool> = Lazy::new(|| { | ||
// Spawn tokio runtime on a single background thread | ||
// enabling IO and timers. | ||
let rt = tokio::runtime::Builder::new_multi_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap(); | ||
let inner = futures::executor::ThreadPool::builder().create().unwrap(); | ||
|
||
ThreadPool { inner, rt } | ||
}); | ||
|
||
impl ThreadPool { | ||
fn spawn(&self, f: impl Future<Output = ()> + Send + 'static) { | ||
let handle = self.rt.handle().clone(); | ||
self.inner.spawn_ok(TokioContext::new(f, handle)); | ||
} | ||
} | ||
} |