Background jobs #1735
-
Although this is not initially one of the main tasks for a web framework, being able to run independent background jobs / tasks is a mechanism that provides a lot of flexibility when creating a web application.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In general, there should rarely be a need for a specialized job/queue system with async Rust. The runtime, and async/await in general, make common "scheduling" tasks fairly straightforward. To start a background task, just spawn a task with tokio::spawn(async {
...
}); To do something periodically, spawn a task with a loop that waits for a timer: tokio::spawn(async {
let mut interval = time::interval(Duration::from_secs(60 * 30));
loop {
interval.tick().await;
// ...
}
}); If you need more control, use the various synchronization primitives in |
Beta Was this translation helpful? Give feedback.
In general, there should rarely be a need for a specialized job/queue system with async Rust. The runtime, and async/await in general, make common "scheduling" tasks fairly straightforward. To start a background task, just spawn a task with
tokio
:To do something periodically, spawn a task with a loop that waits for a timer:
If you need more control, use the various synchronization primitives in
tokio::sync
like the managed queue example.