diff --git a/tokioselect/Cargo.lock b/tokioselect/Cargo.lock new file mode 100644 index 0000000..f1001f0 --- /dev/null +++ b/tokioselect/Cargo.lock @@ -0,0 +1,98 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" + +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "tokio" +version = "1.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c27a64b625de6d309e8c57716ba93021dccf1b3b5c97edd6d3dd2d2135afc0a" +dependencies = [ + "num_cpus", + "pin-project-lite", + "tokio-macros", +] + +[[package]] +name = "tokio-macros" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokioselect" +version = "0.1.0" +dependencies = [ + "tokio", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" diff --git a/tokioselect/Cargo.toml b/tokioselect/Cargo.toml new file mode 100644 index 0000000..b86ee2c --- /dev/null +++ b/tokioselect/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "tokioselect" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio = { version = "1.16.1", features = ["rt", "rt-multi-thread", "macros", "time", "sync"] } diff --git a/tokioselect/src/main.rs b/tokioselect/src/main.rs new file mode 100644 index 0000000..0cc664e --- /dev/null +++ b/tokioselect/src/main.rs @@ -0,0 +1,46 @@ +use tokio::sync::mpsc::channel; +use tokio::time::sleep; +use tokio::time::Duration; + +#[tokio::main] +async fn main() { + let (tx1, mut rx1) = channel(100); + let (tx2, mut rx2) = channel(100); + + tokio::spawn(async move { + println!("I'm worker 1"); + let mut loop_id = 1; + let sleep_time = Duration::from_millis(200); + loop { + let message = format!("This is loop 1-{loop_id}"); + tx1.send(message).await.unwrap(); + sleep(sleep_time).await; + loop_id += 1; + } + }); + + tokio::spawn(async move { + println!("I'm worker 2"); + let mut loop_id = 1; + let sleep_time = Duration::from_millis(300); + loop { + let message = format!("This is loop 2-{loop_id}"); + tx2.send(message).await.unwrap(); + sleep(sleep_time).await; + loop_id += 1; + } + }); + + // This is main + loop { + tokio::select! { + Some(msg) = rx1.recv() => { + println!("Worker 1 said \"{}\"", msg); + } + Some(msg) = rx2.recv() => { + println!("Worker 2 said \"{}\"", msg); + } + else => { break } + }; + } +}