Crates.io | tokio-global |
lib.rs | tokio-global |
version | 0.4.0 |
source | src |
created_at | 2019-03-16 08:21:11.048062 |
updated_at | 2019-12-20 14:43:15.552206 |
description | Global tokio runtime |
homepage | |
repository | https://gitlab.com/Douman/tokio-global |
max_upload_size | |
id | 121324 |
size | 18,997 |
Simple way to create global tokio runtime, available from any place in code
Start runtime and use AutoRuntime
trait to spawn your futures:
use tokio_global::{Runtime, AutoRuntime};
use tokio::io::{AsyncWriteExt, AsyncReadExt};
async fn server() {
let mut listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await.expect("To bind");
let (mut socket, _) = listener.accept().await.expect("To accept connection");
async move {
let mut buf = [0; 1024];
loop {
match socket.read(&mut buf).await {
// socket closed
Ok(0) => return,
Ok(_) => continue,
Err(_) => panic!("Error :("),
};
}
}.spawn().await.expect("Finish listening");
}
async fn client() {
let mut stream = tokio::net::TcpStream::connect("127.0.0.1:8080").await.expect("Connect");
// Write some data.
stream.write_all(b"hello world!").await.expect("Write");
//Stop runtime
Runtime::stop();
}
let _guard = Runtime::default();
let runner = std::thread::spawn(|| {
Runtime::run();
});
server().spawn();
client().spawn();