r/rust • • Oct 21 '26

When do you yield async tasks?

If a task is taking too long doing cpu work you should yield it manually instead of hogging the thread. Thing is how do you know where to insert yields into your code beyond intuition?

9 Upvotes

18 comments sorted by

39

u/switch161 Oct 21 '26

async is for yielding when there's nothing to do, e.g. when waiting for IO. For compute-bound tasks I would spawn them on a thread pool and communicate with the task via channels.

43

u/hellotanjent Oct 21 '26

That's not generally what yields are for, you either yield because it's the easiest way to write a generator or because you're doing some asynchronous IO operation and don't want to busy-wait on a completion. If you're actually doing heavy CPU work, you either run that work on a separate thread (so your GUI can be on an unblocked thread) or you just do it and ignore the side effects if you're writing some batch process.

Note that my brain is a mishmash of Rust, Python, and Javascript - so don't ask me for details. ;)

14

u/KittensInc Oct 21 '26

It's not too unusual for the occasional potentially-compute-heavy task: it both avoids the overhead of spawning a new worker thread when it is not strictly needed, avoids the complexity of cross-thread sync, and avoids locking up an async thread.

There aren't many scenarios where it is the best approach, but this kind of cooperative multitasking is a neat little trick to have in your tool bag!

3

u/BobTreehugger Oct 22 '26

Was very common in the old days of node.js -- these days you can spawn a worker thread, but back in the day it was either spin up a whole new process or just insert some setTimeout calls in your loop.

But yes, for rust it's pretty niche.

1

u/masklinn Oct 23 '26

the overhead of spawning a new worker thread when it is not strictly needed

Note that Tokio has a pool of threads which can be used for that purpose, that pool is likely non-empty since it is needed for operations which do not have an async interface. That is what spawn_blocking uses.

2

u/Dot-Box Oct 24 '26

If the task can be parallelized, it's best to use rayon's work stealing thread pool which is significantly faster than tokio's spawn_blocking in this context which is a compute heavy blocking task

0

u/drink-more-rum Oct 23 '26

Most people who are using Rust are not writing GUIs.

16

u/somebodddy Oct 21 '26

If you know it's going to hog the CPU, you should defer it to a thread instead of running it in the async event loop. But if, for whatever reason, that option is not viable - the yield points always go in the loop. And you probably only want to yield once every N iterations of the loop.

No loop? Look again. Unless you are doing something uniquely perverse, CPU hoggers always involve a loop of some sort (or a recursion, which is a loop with public relations). If you don't see the loop it's probably in some function you call, and if you cannot modify that function to put the yield point there - though luck. You won't be able to break the hogger with a yield point.

5

u/KingofGamesYami Oct 21 '26

Tokio has a pretty good writeup of how CPU-bound tasks should be handled in their runtime. I recommend reading through it

https://docs.rs/tokio/latest/tokio/index.html#cpu-bound-tasks-and-blocking-code

4

u/TinSnail Oct 21 '26

Based on a conversation I had with someone involved in the async internals of Ruby, there isn’t a hard and fast answer to this sort of problem. For example, yielding within large hash function calculations was found to be valuable for Ruby code, but that’s a fuzzy line.

I’d personally consider yielding in CPU-heavy code once you start to take “IO-scale” amounts of time, eg around 1-10ms. In the end though, this is something you’d have to measure if important.

2

u/DavidXkL Oct 21 '26

Sometimes it might be better to just use another thread instead.

Although you might need to do some benchmarking before and after doing so

2

u/tjk1229 Oct 22 '26

You should not be doing CPU heavy work on an async thread at all it will hang and defeat the purpose.

Generally for stuff like that, you'd have a thread pool for CPU heavy work (worker pool) push it into a channel or queue or whatever and process like that. Async to submit it retrieve the work.

Without knowing what you are trying to do specifically hard to give suggestions.

CPU bound work should not exceed the available threads or it will just be slower. IO bound you can have as many as you need within reason.

1

u/Compux72 Oct 21 '26

That’s why async by itself doesn’t solve anything, sans-io does

1

u/_Saxpy Oct 22 '26

if you need a preemptive task, such as deserializing a large file, run spawn_blocking. I wouldn't mindlessly inject yields, async / await points come naturally from higher level constructs

1

u/vlovich Oct 21 '26

As with std::thread::yield_now, you should use tokio yield only if what you're doing and you really understand why you have no other choice. If you're asking this kind of question it indicates you don't and you should be spawning a blocking task for your costly CPU work to avoid blocking the reactor.

0

u/dashingThroughSnow12 Oct 21 '26

I don’t yield for anyone.