Skip to content

Commit

Permalink
use tokio::main!
Browse files Browse the repository at this point in the history
  • Loading branch information
o0Ignition0o committed Jan 31, 2021
1 parent 06b3b14 commit 74f2df5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/bastion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ rand = "0.7.3"
rayon = "1.3.1"
num_cpus = "1.13.0"
# hello_tokio example
tokio = { version="1.1", features = ["time"] }
tokio = { version="1.1", features = ["time", "macros"] }
bastion-executor = { path = "../bastion-executor", features = ["runtime-tokio"] }
14 changes: 13 additions & 1 deletion src/bastion/examples/hello_tokio.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result as AnyResult;
use bastion::prelude::*;
#[cfg(feature = "runtime-tokio")]
use tokio;
Expand All @@ -18,7 +19,8 @@ use tracing::{error, info, warn, Level};
/// Jan 31 11:54:24.473 WARN hello_tokio: the spawn! is complete
/// Jan 31 11:54:34.372 WARN hello_tokio: we're done, stopping the bastion!
#[cfg(feature = "runtime-tokio")]
fn main() {
#[tokio::main]
async fn main() -> AnyResult<()> {
// Initialize tracing logger
// so we get nice output on the console.
let subscriber = tracing_subscriber::fmt()
Expand All @@ -31,21 +33,30 @@ fn main() {
let workers = Bastion::children(|children| {
children.with_exec(|ctx: BastionContext| {
async move {
warn!("just spawned!");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
warn!("Ok let's handle a message now.");
msg! {
ctx.recv().await?,
msg: &'static str => {
// Printing the incoming msg
error!("just received {}", msg);

warn!("sleeping for 2 seconds without using the bastion executor");
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
warn!("and done!");

// let's wait until a tokio powered future is complete
run!(blocking! {
warn!("let's sleep for 5 seconds within a blocking block");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
warn!("awaited 5 seconds");
});
error!("waited for the blocking! to be complete!");

// let's spawn a tokio powered future and move on
spawn! {
warn!("let's sleep for 10 seconds within a spawn block");
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
warn!("the spawn! is complete");
};
Expand Down Expand Up @@ -75,6 +86,7 @@ fn main() {

// We are done, stopping the bastion!
Bastion::stop();
Ok(())
}

#[cfg(not(feature = "runtime-tokio"))]
Expand Down

0 comments on commit 74f2df5

Please sign in to comment.