Tokio: how to setup a task that keep thing tidy?

I have a sort of cache, and each entry has a date. I wrote an async function that parses the cache and look for entries that are too old and removes the entry. How do I set up a task that periodically run this function?

With a std::thread, I think I know how to do it, but with a tokio's task, I'm kinda lost :-/

Use interval to schedule this async task to run periodically. (to be precise, stick it in a loop and call the tick method at the end of it).

2 Likes

As always, tfhank you Alice for your help!

I haven't tried it yet, but doesn't tokio::spawn's result needs awaiting?

No. The tokio::spawn call will make the future start running in the background immediately. The JoinHandle that it returns is only needed if you need to do one or more of the following:

  1. Wait for the task to complete.
  2. You need its return value.
  3. You need to be able to remotely abort the task.
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.