Repository navigation
Reasons to spawn builtin threads vs tokio blocking tasks? #7878
Replies: 3 comments 2 replies
|
When you use |
|
The current documentation doesn’t make this usage distinction very explicit.
|
|
The main thing to correct is this part:
So the tradeoff is more like:
Good let parsed = tokio::task::spawn_blocking(move || {
expensive_sync_parse(bytes)
}).await?;Good dedicated-thread use: let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
while let Ok(job) = rx.recv() {
run_foreverish_blocking_job(job);
}
});Why a dedicated thread can still be the better choice:
So I’d use |
Uh oh!
There was an error while loading. Please reload this page.
Based on my current understanding, a blocking task is basically just a thread that can also yield control with
await(probably not as fast as non-blocking tasks, because it is its own thread), and also haveawaitcalled on its join.I'm only seeing upsides here. If I'm already using non-blocking tasks for stuff, is there any reason to use a built-in thread rather than a blocking task?
All reactions