How manage high-load tasks to give priority for just created task?

58 views Asked by At

We build async http solution on Actix based on Rust. We spawn two task:

  1. Parsing blockchain blocks. This task not consumed much resources cause between blocks enough time for process. This task creates micro task, like "query data", "parse data", "save data".
  2. Review already loaded blocks. This task we run time to time, like once a week, but this task create a high load.

When second task runs it looks like eager, and became block the executing some of small task from 1. If I stopped 2 task, all task from first correctly finished.

The question: how I can temporary stop or pause executing second task for give other small task to be finished?

1

There are 1 answers

0
cafce25 On

Asynchronous tasks are cooperative multitasking, that means your second task has to .await something in it's body, you cannot make it stop or pause from the outside.

CPU bound tasks are not meant to be run in asynchronous tasks anyways, so your best bet is to not put the review task in a task to begin with.

There is a dedicated section on the topic of CPU bound tasks in the tokio documentation, it suggests:

If your code is CPU-bound and you wish to limit the number of threads used to run it, you should use a separate thread pool dedicated to CPU bound tasks. For example, you could consider using the rayon library for CPU-bound tasks.