1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! This example is a copy of the resource_await example, but demonstrates how to use tracing to instrument
//! the nursery.
//!
mod common;
use
{
async_executors :: { AsyncStd } ,
async_nursery :: { Nursery, NurseExt } ,
common :: { DynSendResult, setup_tracing } ,
futures_timer :: { Delay } ,
std :: { time::Duration } ,
tracing_futures :: { Instrument } ,
// just because we use tracing as a feature name, you don't have to do this.
//
tracing_crate as tracing ,
tracing :: { info } ,
};
async fn resource_await( amount: usize ) -> DynSendResult<()>
{
let (nursery, output) = Nursery::new( AsyncStd ); info!( "nursery created" );
let nursery = nursery.instrument( tracing::info_span!( "tracing-example" ) );
for _ in 0..amount
{
nursery.nurse( slow() )?;
}
// This is necessary. Since we could keep spawning tasks even after starting to poll
// the output, it can't know that we are done, unless we drop all senders or call
// `close_nursery`. If we don't, the await below deadlocks.
//
drop(nursery);
// Resolves when all spawned tasks are done.
//
output.await;
info!( "drop Nursery and NurseryStream" );
Ok(())
}
async fn slow() -> DynSendResult<()>
{
info!( "spawned slow" );
Delay::new( Duration::from_secs(1) ).await;
Ok(())
}
#[ async_std::main ]
//
async fn main() -> DynSendResult<()>
{
setup_tracing();
resource_await( 5 ).await?;
Ok(())
}