tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.49k stars 2.44k forks source link

Add a LocalRuntime for replacing Runtime+LocalSet #6739

Open Darksonn opened 1 month ago

Darksonn commented 1 month ago

Currently, to spawn !Send tasks on Tokio you must use a combination of a current-thread runtime and LocalSet to make that work. However, tasks on a LocalSet are separate from the rest of the runtime in an uncomfortable way. For example:

For the above reasons, I propose introducing a new type called LocalRuntime which behaves exactly like a current-thread Runtime with the following exceptions:

Since Runtime and LocalSet share a fair amount of their internals, the implementation of current-thread should not require changes to support this.

Noah-Kennedy commented 4 weeks 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.

Noah-Kennedy commented 3 weeks ago

I've opened a PR to add this as an unstable API: https://github.com/tokio-rs/tokio/pull/6808

carllerche commented 2 weeks ago

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.

Noah-Kennedy commented 2 weeks ago

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: