-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a LocalRuntime for replacing Runtime+LocalSet #6739
Comments
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. |
This change adds LocalRuntime, a new unstable runtime type which cannot be transferred across thread boundaries and supports spawn_local when called from the thread which owns the runtime. The initial set of docs for this are iffy. Documentation is absent right now at the module level, with the docs for the LocalRuntime struct itself being somewhat duplicative of those for the `Runtime` type. This can probably be addressed later as stabilization nears. This API has a few interesting implementation details: - because it was considered beneficial to reuse the same Handle as the normal runtime, it is possible to call spawn_local from a runtime context while on a different thread from the one which drives the runtime and owns it. This forces us to check the thread ID before attempting a local spawn. - An empty LocalOptions struct is passed into the build_local method in order to build the runtime. This will eventually have stuff in it like hooks. Relates to #6739.
I've opened a PR to add this as an unstable API: #6808 |
Generally, I agree with the reasoning for adding |
I think the decision tree here comes down to several things:
|
This change adds LocalRuntime, a new unstable runtime type which cannot be transferred across thread boundaries and supports spawn_local when called from the thread which owns the runtime. The initial set of docs for this are iffy. Documentation is absent right now at the module level, with the docs for the LocalRuntime struct itself being somewhat duplicative of those for the `Runtime` type. This can probably be addressed later as stabilization nears. This API has a few interesting implementation details: - because it was considered beneficial to reuse the same Handle as the normal runtime, it is possible to call spawn_local from a runtime context while on a different thread from the one which drives the runtime and owns it. This forces us to check the thread ID before attempting a local spawn. - An empty LocalOptions struct is passed into the build_local method in order to build the runtime. This will eventually have stuff in it like hooks. Relates to #6739.
This change adds LocalRuntime, a new unstable runtime type which cannot be transferred across thread boundaries and supports spawn_local when called from the thread which owns the runtime. The initial set of docs for this are iffy. Documentation is absent right now at the module level, with the docs for the LocalRuntime struct itself being somewhat duplicative of those for the `Runtime` type. This can probably be addressed later as stabilization nears. This API has a few interesting implementation details: - because it was considered beneficial to reuse the same Handle as the normal runtime, it is possible to call spawn_local from a runtime context while on a different thread from the one which drives the runtime and owns it. This forces us to check the thread ID before attempting a local spawn. - An empty LocalOptions struct is passed into the build_local method in order to build the runtime. This will eventually have stuff in it like hooks. Relates to #6739.
This change adds LocalRuntime, a new unstable runtime type which cannot be transferred across thread boundaries and supports spawn_local when called from the thread which owns the runtime. The initial set of docs for this are iffy. Documentation is absent right now at the module level, with the docs for the LocalRuntime struct itself being somewhat duplicative of those for the `Runtime` type. This can be addressed later as stabilization nears. This API has a few interesting implementation details: - because it was considered beneficial to reuse the same Handle as the normal runtime, it is possible to call spawn_local from a runtime context while on a different thread from the one which drives the runtime and owns it. This forces us to check the thread ID before attempting a local spawn. - An empty LocalOptions struct is passed into the build_local method in order to build the runtime. This will eventually have stuff in it like hooks. Relates to #6739.
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.The text was updated successfully, but these errors were encountered: