Created
August 13, 2019 15:49
-
-
Save Eliah-Lakhin/9514b4798a2aed84615dbde807dbafa5 to your computer and use it in GitHub Desktop.
Waking up Tokio Blocking Thread attempt
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
#![feature(async_await)] | |
use std::error::Error; | |
use tokio::spawn; | |
use tokio::prelude::*; | |
use tokio::future::poll_fn; | |
use tokio_threadpool::blocking; | |
use tokio::sync::oneshot::{Sender, Receiver, channel}; | |
#[tokio::main] | |
async fn main() { | |
let (init_tx, mut init_rx) = channel::<(usize, Sender<bool>)>(); | |
spawn(async move { | |
poll_fn(|_| blocking(|| { | |
let f = async { | |
println!("In thread"); | |
// A point of failure | |
let (x, done_tx) = init_rx.try_recv().unwrap(); | |
println!("X received: {}", x); | |
done_tx.send(true); | |
}; | |
tokio_executor::enter().unwrap().block_on(f); | |
})).await; | |
}); | |
// Removing this line makes code work | |
std::thread::sleep(std::time::Duration::from_secs(5)); | |
println!("Before init"); | |
let (done_tx, done_rx) = channel(); | |
init_tx.send((100, done_tx)).map_err(|_| panic!("Failed to send")); | |
println!("After init"); | |
done_rx.await; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An error:
tokio, tokio-threadpool, tokio-executor versions is
0.2.0-alpha.1