-
Notifications
You must be signed in to change notification settings - Fork 639
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
Consider providing a way to spawn tasks using a thread-local. #830
Comments
I think the ergonomic issues around returning a future rather than having a function that resolves immediately will be mostly alleviated by async/await. However, the |
Currently, `tokio::spawn` matched the `spawn` function from futures 0.2. However, this adds additional ergonomic overhead and removes the ability to spawn from a drop fn. See rust-lang/futures-rs#830. This patch switches the behavior to access the thread-local variable referencing the default executor directly in the `spawn` function.
Currently, `tokio::spawn` matched the `spawn` function from futures 0.2. However, this adds additional ergonomic overhead and removes the ability to spawn from a drop fn. See rust-lang/futures-rs#830. This patch switches the behavior to access the thread-local variable referencing the default executor directly in the `spawn` function.
I'm going to close this for the time being, as per @cramertj's comment, and given that Tokio currently has a workaround. We can revisit this as needed later on, when things with async/await are more clear. |
Wouldn't this be possible to tap the current executor thanks to the |
Currently, the strategy for spawning is to require a
&mut Context
. This means the freespawn
function does not actually spawn, but returns a future that spawns when it is polled.This creates a number of ergonomic issues.
Spawning in a function.
First, this currently is a Tokio example:
Moving to the proposed solution, this becomes:
Spawning on Drop.
The second problem is that it becomes impossible to spawn in implementations of drop. This is a problem as one pattern for dealing with dropping types that require further async processing to "finalize" is to spawn the finalization logic in a new task. This pattern becomes no longer possible, instead forcing all types to impl a
poll_close
strategy. This isn't really great as this becomes something that must be threaded through all layers.Imagine type
Foo
implements traitHello
andBar<T: Hello>
. Currently, the above strategy can be used to perform any finalization logic necessary withoutBar
having to opt-in to running this logic before it drops. However, this is no longer possible requiring the addition of somePollClose
trait....Thread-local solution.
Using a thread-local to store the ref to the executor solves both problems.
The text was updated successfully, but these errors were encountered: