| Crates.io | moosicbox_task |
| lib.rs | moosicbox_task |
| version | 0.1.4 |
| created_at | 2024-10-04 00:11:18.654545+00 |
| updated_at | 2025-07-21 19:10:20.165047+00 |
| description | MoosicBox task utilities package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1395843 |
| size | 29,545 |
Simple task management utilities for the MoosicBox ecosystem, providing basic async task spawning with optional naming and profiling support for Tokio-based applications.
Add this to your Cargo.toml:
[dependencies]
moosicbox_task = "0.1.1"
# Enable profiling support
moosicbox_task = { version = "0.1.1", features = ["profiling"] }
use moosicbox_task::{spawn, spawn_blocking};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Spawn async task with a name
let async_task = spawn("background-processing", async {
println!("Running async task");
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
"async result"
});
// Spawn blocking task with a name
let blocking_task = spawn_blocking("cpu-intensive-work", || {
println!("Running blocking task");
std::thread::sleep(std::time::Duration::from_secs(1));
"blocking result"
});
// Wait for both tasks to complete
let async_result = async_task.await?;
let blocking_result = blocking_task.await?;
println!("Async result: {}", async_result);
println!("Blocking result: {}", blocking_result);
Ok(())
}
use moosicbox_task::{spawn_on, spawn_blocking_on};
use tokio::runtime::Handle;
async fn spawn_on_runtime() -> Result<(), Box<dyn std::error::Error>> {
let handle = Handle::current();
// Spawn on specific runtime handle
let task = spawn_on("named-task", &handle, async {
println!("Running on specific runtime");
"result"
});
// Spawn blocking on specific runtime
let blocking_task = spawn_blocking_on("blocking-task", &handle, || {
println!("Blocking work on specific runtime");
42
});
let result = task.await?;
let blocking_result = blocking_task.await?;
println!("Results: {}, {}", result, blocking_result);
Ok(())
}
use moosicbox_task::{spawn_local, spawn_local_on};
use tokio::task::LocalSet;
async fn local_task_example() -> Result<(), Box<dyn std::error::Error>> {
let local_set = LocalSet::new();
// Spawn task on local set
let task = local_set.run_until(async {
let task = spawn_local("local-task", async {
println!("Running on local task set");
"local result"
});
task.await
}).await?;
println!("Local task result: {}", task);
// Or spawn on specific local set
let result = spawn_local_on("named-local-task", &local_set, async {
"local set result"
});
// Run the local set
let output = local_set.run_until(result).await?;
println!("Local set output: {}", output);
Ok(())
}
use moosicbox_task::block_on;
// Block on async operation (useful in sync contexts)
fn sync_function() -> Result<String, Box<dyn std::error::Error>> {
let result = block_on("sync-to-async", async {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
"converted to sync"
});
Ok(result)
}
use moosicbox_task::spawn_on_opt;
async fn maybe_spawn_on_handle(handle: Option<&tokio::runtime::Handle>) {
// Spawn on handle if provided, otherwise use current runtime
let task = spawn_on_opt("flexible-task", handle, async {
"flexible result"
});
let result = task.await.unwrap();
println!("Result: {}", result);
}
When the profiling feature is enabled, tasks automatically get profiling scopes:
use moosicbox_task::spawn;
// This task will have profiling information when profiling is enabled
let task = spawn("profiled-task", async {
// Work here will be tracked in profiling tools
expensive_computation().await
});
spawn(name, future): Spawn named async task on current runtimespawn_on(name, handle, future): Spawn on specific runtime handlespawn_on_opt(name, handle, future): Spawn on optional handlespawn_blocking(name, function): Spawn blocking task on current runtimespawn_blocking_on(name, handle, function): Spawn blocking task on specific runtimespawn_local(name, future): Spawn task on current local setspawn_local_on(name, local_set, future): Spawn on specific local setblock_on(name, future): Block current thread until future completesblock_on_runtime(name, handle, future): Block using specific runtimetokio: For async task spawning and runtime managementfutures: For Future traitlog: For optional debug loggingprofiling: For optional profiling supportThis library provides simple, named task spawning utilities that make debugging and profiling easier in async Rust applications.