Last active
April 20, 2020 17:34
-
-
Save fteychene/b66520dcade4f9fd9092f4e38c2e014b to your computer and use it in GitHub Desktop.
Test multi runtime and spwn_blocking in Tokio
This file contains hidden or 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
[package] | |
name = "test_multi_runtime" | |
version = "0.1.0" | |
authors = ["fteychene <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
anyhow = "1.0.28" | |
tokio = { version = "0.2", features = ["full"] } | |
serde = "1.0" | |
serde_dhall = "0.5" | |
rand = "0.7" |
This file contains hidden or 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
use anyhow::{Error, Context}; | |
use std::time::Instant; | |
use tokio::task::{block_in_place}; | |
const CONFIG: &'static str = r#" | |
let Prelude = https://prelude.dhall-lang.org/package.dhall | |
let hack = [ True, False ] | |
in hack | |
"#; | |
const LIGHT_CONFIG : &'static str = r#" | |
let hack = [ True, False ] | |
in hack | |
"#; | |
const FAIL_CONFIG: &'static str = r#" | |
let Mock = https://raw.githubusercontent.com/dhall-mock/dhall-mock/master/dhall/Mock/package.dhall | |
let expectations = [ | |
{ request = { method = Some Mock.HttpMethod.GET | |
, path = Some "/greet/warcraft3" | |
} | |
, response = { statusCode = Some +200 | |
, statusReason = None Text | |
, body = Some "Oui monseigneur" | |
} | |
} | |
] | |
in expectation | |
"#; | |
async fn load_configuration(config: &'static str) -> Result<(), Error> { | |
println!("Received config to load"); | |
block_in_place( move || { | |
println!("Start loading config"); | |
let now = Instant::now(); | |
let result: Result<(), Error> = serde_dhall::from_str(config) | |
.parse() | |
.context("Error parsing dhall configuration"); | |
println!("Loaded in {} secs", now.elapsed().as_secs()); | |
result | |
}) | |
} | |
fn main() -> Result<(), Error> { | |
println!("Start loading massive import"); | |
let loading_rt = tokio::runtime::Builder::new() | |
.threaded_scheduler() | |
.thread_stack_size(8*1024*1024) | |
.core_threads(1) | |
.max_threads(3) | |
.build()?; | |
let mut rt = tokio::runtime::Builder::new() | |
.threaded_scheduler() | |
.build()?; | |
let result: Result<(), Error> = rt.block_on(async { loading_rt.spawn(load_configuration(LIGHT_CONFIG)).await? }); | |
match result { | |
Ok(()) => println!("Result config loaded π"), | |
Err(e) => eprintln!("Error during compilation : {:#}", e) | |
} | |
let result: Result<(), Error> = rt.block_on(async { | |
loading_rt.spawn(load_configuration(FAIL_CONFIG)).await? | |
}); | |
match result { | |
Ok(()) => println!("Result config loaded π"), | |
Err(e) => eprintln!("Error during compilation : {:#}", e) | |
} | |
let result: Result<(), Error> = rt.block_on(async { | |
loading_rt.spawn(load_configuration(CONFIG)).await? | |
}); | |
match result { | |
Ok(()) => println!("Result config loaded π"), | |
Err(e) => eprintln!("Error during compilation : {:#}", e) | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment