| 1 | // This example shows how to use the tokio runtime with any other executor |
| 2 | // |
| 3 | //It takes advantage from RuntimeExt which provides the extension to customize your |
| 4 | //runtime. |
| 5 | |
| 6 | use tokio::net::TcpListener; |
| 7 | use tokio::runtime::Builder; |
| 8 | use tokio::sync::oneshot; |
| 9 | use tokio_util::context::RuntimeExt; |
| 10 | |
| 11 | fn main() { |
| 12 | let (tx, rx) = oneshot::channel(); |
| 13 | let rt1 = Builder::new_multi_thread() |
| 14 | .worker_threads(1) |
| 15 | // no timer! |
| 16 | .build() |
| 17 | .unwrap(); |
| 18 | let rt2 = Builder::new_multi_thread() |
| 19 | .worker_threads(1) |
| 20 | .enable_all() |
| 21 | .build() |
| 22 | .unwrap(); |
| 23 | |
| 24 | // Without the `HandleExt.wrap()` there would be a panic because there is |
| 25 | // no timer running, since it would be referencing runtime r1. |
| 26 | let _ = rt1.block_on(rt2.wrap(async move { |
| 27 | let listener = TcpListener::bind("0.0.0.0:0" ).await.unwrap(); |
| 28 | println!("addr: {:?}" , listener.local_addr()); |
| 29 | tx.send(()).unwrap(); |
| 30 | })); |
| 31 | futures::executor::block_on(rx).unwrap(); |
| 32 | } |
| 33 | |