|
1 | 1 | # Ticked Async Executor
|
2 | 2 |
|
3 |
| -Rust based Async Executor which executes woken tasks only when it is ticked |
| 3 | +Async Local Executor which executes woken tasks only when it is ticked |
4 | 4 |
|
5 |
| -# Example |
| 5 | +# Usage |
| 6 | + |
| 7 | +## Default Local Executor |
6 | 8 |
|
7 | 9 | ```rust
|
| 10 | +use ticked_async_executor::*; |
| 11 | + |
| 12 | +const DELTA: f64 = 1000.0 / 60.0; |
| 13 | + |
8 | 14 | let executor = TickedAsyncExecutor::default();
|
9 | 15 |
|
10 | 16 | executor.spawn_local("MyIdentifier", async move {}).detach();
|
11 | 17 |
|
12 |
| -// Make sure to tick your executor to run the tasks |
13 |
| -executor.tick(DELTA, LIMIT); |
| 18 | +// Tick your executor to run the tasks |
| 19 | +assert_eq!(executor.num_tasks(), 1); |
| 20 | +executor.tick(DELTA, None); |
| 21 | +assert_eq!(executor.num_tasks(), 0); |
| 22 | +``` |
| 23 | + |
| 24 | +## Split Local Executor |
| 25 | + |
| 26 | +```rust |
| 27 | +use ticked_async_executor::*; |
| 28 | + |
| 29 | +const DELTA: f64 = 1000.0 / 60.0; |
| 30 | + |
| 31 | +let (spawner, ticker) = SplitTickedAsyncExecutor::default(); |
| 32 | + |
| 33 | +spawner.spawn_local("MyIdentifier", async move {}).detach(); |
| 34 | + |
| 35 | +// Tick your ticker to run the tasks |
| 36 | +assert_eq!(spawner.num_tasks(), 1); |
| 37 | +ticker.tick(DELTA, None); |
| 38 | +assert_eq!(spawner.num_tasks(), 0); |
14 | 39 | ```
|
15 | 40 |
|
16 |
| -# Limitation |
| 41 | +## Limit the number of woken tasks run per tick |
| 42 | + |
| 43 | +```rust |
| 44 | +use ticked_async_executor::*; |
| 45 | + |
| 46 | +const DELTA: f64 = 1000.0 / 60.0; |
| 47 | + |
| 48 | +let executor = TickedAsyncExecutor::default(); |
| 49 | + |
| 50 | +executor.spawn_local("MyIdentifier1", async move {}).detach(); |
| 51 | +executor.spawn_local("MyIdentifier2", async move {}).detach(); |
| 52 | + |
| 53 | +// Runs upto 1 woken tasks per tick |
| 54 | +assert_eq!(executor.num_tasks(), 2); |
| 55 | +executor.tick(DELTA, Some(1)); |
| 56 | +assert_eq!(executor.num_tasks(), 1); |
| 57 | +executor.tick(DELTA, Some(1)); |
| 58 | +assert_eq!(executor.num_tasks(), 0); |
| 59 | +``` |
| 60 | + |
| 61 | +# Caveats |
| 62 | + |
| 63 | +- Uses the `smol` ecosystem |
| 64 | +- Ensure that tasks are spawned on the same thread as the one that initializes the executor |
| 65 | + |
| 66 | +# Roadmap |
17 | 67 |
|
18 |
| -- Does not work with the tokio runtime and async constructs that use the tokio runtime internally |
| 68 | +- [x] TickedAsyncExecutor |
| 69 | +- [x] SplitTickedAsyncExecutor |
| 70 | + - Similar to the channel API, but spawner and ticker cannot be moved to different threads |
| 71 | +- [ ] Tracing |
0 commit comments