-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Update documentation on block_in_place #3753
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,23 @@ cfg_rt_multi_thread! { | |
/// | ||
/// In general, issuing a blocking call or performing a lot of compute in a | ||
/// future without yielding is not okay, as it may prevent the executor from | ||
/// driving other futures forward. This function runs the closure on the | ||
/// current thread by having the thread temporarily cease from being a core | ||
/// thread, and turns it into a blocking thread. See the [CPU-bound tasks | ||
/// and blocking code][blocking] section for more information. | ||
/// | ||
/// Although this function avoids starving other independently spawned | ||
/// tasks, any other code running concurrently in the same task will be | ||
/// suspended during the call to `block_in_place`. This can happen e.g. when | ||
/// using the [`join!`] macro. To avoid this issue, use [`spawn_blocking`] | ||
/// instead. | ||
/// | ||
/// Note that this function can only be used when using the `multi_thread` runtime. | ||
/// driving other tasks forward. By using this function, the executor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Calling this function informs the executor that the currently executing task is about to block the thread" maybe? TOLI |
||
/// knows that the task it is currently executing is about to block the | ||
/// thread, and the executor is able to hand off any other tasks it has to a | ||
/// new worker thread. See the [CPU-bound tasks and blocking code][blocking] | ||
/// section for more information. | ||
/// | ||
/// Be aware that although this function avoids starving other independently | ||
/// spawned tasks, any other code running concurrently in the same task will | ||
/// be suspended during the call to `block_in_place`. This can happen e.g. | ||
/// when using the [`join!`] macro. To avoid this issue, use | ||
/// [`spawn_blocking`] instead of `block_in_place`. | ||
/// | ||
/// Note that this function cannot be used within a [`current_thread`] runtime | ||
/// because in this case there are no other worker threads to hand off tasks | ||
/// to. On the other hand, calling the function outside a runtime is | ||
/// allowed. In this case, `block_in_place` just calls the provided closure | ||
/// normally. | ||
/// | ||
/// Code running behind `block_in_place` cannot be cancelled. When you shut | ||
/// down the executor, it will wait indefinitely for all blocking operations | ||
|
@@ -43,6 +48,28 @@ cfg_rt_multi_thread! { | |
/// }); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Code running inside `block_in_place` may use `block_on` to reenter the | ||
/// async context. | ||
/// | ||
/// ``` | ||
/// use tokio::task; | ||
/// use tokio::runtime::Handle; | ||
/// | ||
/// # async fn docs() { | ||
/// task::block_in_place(move || { | ||
/// Handle::current().block_on(async move { | ||
/// // do something async | ||
/// }); | ||
/// }); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function panics if called from a [`current_thread`] runtime. | ||
/// | ||
/// [`current_thread`]: fn@crate::runtime::Builder::new_current_thread | ||
pub fn block_in_place<F, R>(f: F) -> R | ||
where | ||
F: FnOnce() -> R, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated, but "is not okay" probably can be reworded to: "is problematic"