Open Darksonn opened 1 month ago
I am strongly in favor of this proposal, and would be happy to take some time and write a PR to add this to tokio_unstable.
I've opened a PR to add this as an unstable API: https://github.com/tokio-rs/tokio/pull/6808
Generally, I agree with the reasoning for adding LocalSet
. We would need to think about how to position this with the current thread runtime. Both LocalRuntime and CurrentThreadRuntime have a lot of overlap. So, how to pick each one would need to be clear.
Generally, I agree with the reasoning for adding
LocalSet
. We would need to think about how to position this with the current thread runtime. Both LocalRuntime and CurrentThreadRuntime have a lot of overlap. So, how to pick each one would need to be clear.
I think the decision tree here comes down to several things:
Currently, to spawn
!Send
tasks on Tokio you must use a combination of a current-thread runtime andLocalSet
to make that work. However, tasks on aLocalSet
are separate from the rest of the runtime in an uncomfortable way. For example:LocalSet
cantokio::spawn
to get onto the runtime, but once you're on the runtime, you can no longerspawn_local
to get back into theLocalSet
.LocalSet
behave like a single task usingFuturesUnordered
, so various runtime options such asevent_interval
behave in a very surprising way by counting many tasks at once.LocalSet
has been shown to involve considerable performance overhead compared to unsafely spawning the!Send
tasks onto a current-thread runtime.For the above reasons, I propose introducing a new type called
LocalRuntime
which behaves exactly like a current-threadRuntime
with the following exceptions:LocalRuntime
type is!Send
to enable spawning!Send
tasks directly onto the runtime.LocalRuntime
, thetokio::spawn
andspawn_local
functions have identical behavior.spawn_local
are scheduled in the same way as normaltokio::spawn
tasks.Since
Runtime
andLocalSet
share a fair amount of their internals, the implementation of current-thread should not require changes to support this.