Add FuturesUnordered example
This commit is contained in:
parent
7b4a79c803
commit
0f538ea7fc
13
Cargo.lock
generated
13
Cargo.lock
generated
@ -146,6 +146,15 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
|
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-unordered"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"futures",
|
||||||
|
"kameo",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-util"
|
name = "futures-util"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@ -446,9 +455,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.40.0"
|
version = "1.41.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
|
checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"backtrace",
|
"backtrace",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = ["alpha", "kameo-actors", "my-actors"]
|
members = ["alpha", "futures-unordered", "kameo-actors", "my-actors"]
|
||||||
|
9
futures-unordered/Cargo.toml
Normal file
9
futures-unordered/Cargo.toml
Normal 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"] }
|
22
futures-unordered/src/main.rs
Normal file
22
futures-unordered/src/main.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user