Skip to content

Background jobs #1735

Answered by SergioBenitez
rtubio asked this question in Ideas
Jun 30, 2021 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

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:

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 tokio::sync like the managed queue example.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@rtubio
Comment options

@SergioBenitez
Comment options

@rtubio
Comment options

Answer selected by SergioBenitez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants