Add FuturesUnordered example

This commit is contained in:
Maciej Krzyżanowski 2024-11-11 10:22:57 +01:00
parent 7b4a79c803
commit 0f538ea7fc
4 changed files with 43 additions and 3 deletions

13
Cargo.lock generated
View File

@ -146,6 +146,15 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-unordered"
version = "0.1.0"
dependencies = [
"futures",
"kameo",
"tokio",
]
[[package]]
name = "futures-util"
version = "0.3.31"
@ -446,9 +455,9 @@ dependencies = [
[[package]]
name = "tokio"
version = "1.40.0"
version = "1.41.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33"
dependencies = [
"backtrace",
"bytes",

View File

@ -1,3 +1,3 @@
[workspace]
resolver = "2"
members = ["alpha", "kameo-actors", "my-actors"]
members = ["alpha", "futures-unordered", "kameo-actors", "my-actors"]

View File

@ -0,0 +1,9 @@
[package]
name = "futures-unordered"
version = "0.1.0"
edition = "2021"
[dependencies]
futures = "0.3.31"
kameo = "0.12.2"
tokio = { version = "1.41.1", features = ["full"] }

View File

@ -0,0 +1,22 @@
async fn func_wait(name: &str, millis: u64) {
println!("{name} started");
tokio::time::sleep(Duration::from_millis(millis)).await;
println!("{name} finished");
}
#[tokio::main]
async fn main() {
let actor_a = kameo::spawn(ActorA {});
let first = actor_a.ask(AMessage).send();
let second = actor_a.ask(AMessage).send();
let mut waiter = futures_unordered::FuturesUnordered::new();
waiter.push(func_wait("first", 2000));
waiter.push(func_wait("second", 3000));
waiter.push(func_wait("third", 1000));
while let Some(res) = waiter.next().await {
println!("{:?}", res);
}
}