Skip to content
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 Handle::spawn_blocking method #2501

Merged
merged 1 commit into from
May 7, 2020

Conversation

acfoltzer
Copy link
Contributor

This follows a similar pattern to Handle::spawn to add the blocking spawn capabilities to Handle. I took a fairly direct approach to changing the visibilities within the blocking modules to get this to work out, and I would understand if you'd rather have things locked down some more. Please let me know if there are any changes you'd like to see there.

Motivation

We have an app with multiple runtimes, so we prefer to perform operations explicitly with handles rather than using the tokio::task::* spawn operators. However, handles don't currently offer an API to spawn a blocking computation.

Solution

Adapts the implementation of task::spawn_blocking to use the blocking spawner that's already in the handle, guarded by cfg(blocking).

This follows a similar pattern to `Handle::spawn` to add the blocking spawn capabilities to
`Handle`. I took a fairly direct approach to changing the visibilities within the blocking modules
to get this to work out, and I would understand if you'd rather have things locked down some
more. Please let me know if there are any changes you'd like to see there.
@Darksonn Darksonn added A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-task Module: tokio/task labels May 7, 2020
@hawkw
Copy link
Member

hawkw commented May 7, 2020

The motivation definitely makes sense to me 👍

I noticed that the Windows CI tests failed, but I think that was unrelated — I've restarted them, and it looks like they're okay now. I'll take a closer look at the code soon!

@carllerche carllerche merged commit 07533a5 into tokio-rs:master May 7, 2020
@acfoltzer acfoltzer deleted the acf/handle-spawn-blocking branch May 20, 2020 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-task Module: tokio/task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants