212 lines
No EOL
11 KiB
HTML
212 lines
No EOL
11 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<meta name="generator" content="rustdoc">
|
||
<meta name="description" content="API documentation for the Rust `blocking` fn in crate `tokio_threadpool`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, blocking">
|
||
|
||
<title>tokio_threadpool::blocking - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../normalize.css">
|
||
<link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
|
||
|
||
<link rel="stylesheet" type="text/css" href="../dark.css">
|
||
<link rel="stylesheet" type="text/css" href="../main.css" id="themeStyle">
|
||
<script src="../storage.js"></script>
|
||
|
||
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc fn">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
<div class="sidebar-menu">☰</div>
|
||
|
||
<p class='location'><a href='index.html'>tokio_threadpool</a></p><script>window.sidebarCurrent = {name: 'blocking', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
||
</nav>
|
||
|
||
<div class="theme-picker">
|
||
<button id="theme-picker" aria-label="Pick another theme!">
|
||
<img src="../brush.svg" width="18" alt="Pick another theme!">
|
||
</button>
|
||
<div id="theme-choices"></div>
|
||
</div>
|
||
<script src="../theme.js"></script>
|
||
<nav class="sub">
|
||
<form class="search-form js-only">
|
||
<div class="search-container">
|
||
<input class="search-input" name="search"
|
||
autocomplete="off"
|
||
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
||
type="search">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content">
|
||
<h1 class='fqn'><span class='in-band'>Function <a href='index.html'>tokio_threadpool</a>::<wbr><a class="fn" href=''>blocking</a></span><span class='out-of-band'><span id='render-detail'>
|
||
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
|
||
[<span class='inner'>−</span>]
|
||
</a>
|
||
</span><a class='srclink' href='../src/tokio_threadpool/blocking.rs.html#117-150' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust fn'>pub fn blocking<F, T>(f: F) -> <a class="type" href="../futures/poll/type.Poll.html" title="type futures::poll::Poll">Poll</a><T, <a class="struct" href="../tokio_threadpool/struct.BlockingError.html" title="struct tokio_threadpool::BlockingError">BlockingError</a>> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> T, </span></pre><div class='docblock'><p>Enter a blocking section of code.</p>
|
||
<p>The <code>blocking</code> function annotates a section of code that performs a blocking
|
||
operation, either by issuing a blocking syscall or by performing a long
|
||
running CPU-bound computation.</p>
|
||
<p>When the <code>blocking</code> function enters, it hands off the responsibility of
|
||
processing the current work queue to another thread. Then, it calls the
|
||
supplied closure. The closure is permitted to block indefinitely.</p>
|
||
<p>If the maximum number of concurrent <code>blocking</code> calls has been reached, then
|
||
<code>NotReady</code> is returned and the task is notified once existing <code>blocking</code>
|
||
calls complete. The maximum value is specified when creating a thread pool
|
||
using <a href="struct.Builder.html#method.max_blocking"><code>Builder::max_blocking</code></a></p>
|
||
<h1 id="return" class="section-header"><a href="#return">Return</a></h1>
|
||
<p>When the blocking closure is executed, <code>Ok(T)</code> is returned, where <code>T</code> is the
|
||
closure's return value.</p>
|
||
<p>If the thread pool has shutdown, <code>Err</code> is returned.</p>
|
||
<p>If the number of concurrent <code>blocking</code> calls has reached the maximum,
|
||
<code>Ok(NotReady)</code> is returned and the current task is notified when a call to
|
||
<code>blocking</code> will succeed.</p>
|
||
<p>If <code>blocking</code> is called from outside the context of a Tokio thread pool,
|
||
<code>Err</code> is returned.</p>
|
||
<h1 id="background" class="section-header"><a href="#background">Background</a></h1>
|
||
<p>By default, the Tokio thread pool expects that tasks will only run for short
|
||
periods at a time before yielding back to the thread pool. This is the basic
|
||
premise of cooperative multitasking.</p>
|
||
<p>However, it is common to want to perform a blocking operation while
|
||
processing an asynchronous computation. Examples of blocking operation
|
||
include:</p>
|
||
<ul>
|
||
<li>Performing synchronous file operations (reading and writing).</li>
|
||
<li>Blocking on acquiring a mutex.</li>
|
||
<li>Performing a CPU bound computation, like cryptographic encryption or
|
||
decryption.</li>
|
||
</ul>
|
||
<p>One option for dealing with blocking operations in an asynchronous context
|
||
is to use a thread pool dedicated to performing these operations. This not
|
||
ideal as it requires bidirectional message passing as well as a channel to
|
||
communicate which adds a level of buffering.</p>
|
||
<p>Instead, <code>blocking</code> hands off the responsibility of processing the work queue
|
||
to another thread. This hand off is light compared to a channel and does not
|
||
require buffering.</p>
|
||
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
|
||
<p>Block on receiving a message from a <code>std</code> channel. This example is a little
|
||
silly as using the non-blocking channel from the <code>futures</code> crate would make
|
||
more sense. The blocking receive can be replaced with any blocking operation
|
||
that needs to be performed.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
|
||
<span class="kw">use</span> <span class="ident">tokio_threadpool</span>::{<span class="ident">ThreadPool</span>, <span class="ident">blocking</span>};
|
||
|
||
<span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">Future</span>;
|
||
<span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">future</span>::{<span class="ident">lazy</span>, <span class="ident">poll_fn</span>};
|
||
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">mpsc</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">time</span>::<span class="ident">Duration</span>;
|
||
|
||
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">main</span>() {
|
||
<span class="comment">// This is a *blocking* channel</span>
|
||
<span class="kw">let</span> (<span class="ident">tx</span>, <span class="ident">rx</span>) <span class="op">=</span> <span class="ident">mpsc</span>::<span class="ident">channel</span>();
|
||
|
||
<span class="comment">// Spawn a thread to send a message</span>
|
||
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">||</span> {
|
||
<span class="ident">thread</span>::<span class="ident">sleep</span>(<span class="ident">Duration</span>::<span class="ident">from_millis</span>(<span class="number">500</span>));
|
||
<span class="ident">tx</span>.<span class="ident">send</span>(<span class="string">"hello"</span>).<span class="ident">unwrap</span>();
|
||
});
|
||
|
||
<span class="kw">let</span> <span class="ident">pool</span> <span class="op">=</span> <span class="ident">ThreadPool</span>::<span class="ident">new</span>();
|
||
|
||
<span class="ident">pool</span>.<span class="ident">spawn</span>(<span class="ident">lazy</span>(<span class="kw">move</span> <span class="op">||</span> {
|
||
<span class="comment">// Because `blocking` returns `Poll`, it is intended to be used</span>
|
||
<span class="comment">// from the context of a `Future` implementation. Since we don't</span>
|
||
<span class="comment">// have a complicated requirement, we can use `poll_fn` in this</span>
|
||
<span class="comment">// case.</span>
|
||
<span class="ident">poll_fn</span>(<span class="kw">move</span> <span class="op">||</span> {
|
||
<span class="ident">blocking</span>(<span class="op">||</span> {
|
||
<span class="kw">let</span> <span class="ident">msg</span> <span class="op">=</span> <span class="ident">rx</span>.<span class="ident">recv</span>().<span class="ident">unwrap</span>();
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"message = {}"</span>, <span class="ident">msg</span>);
|
||
}).<span class="ident">map_err</span>(<span class="op">|</span>_<span class="op">|</span> <span class="macro">panic</span><span class="macro">!</span>(<span class="string">"the threadpool shut down"</span>))
|
||
})
|
||
}));
|
||
|
||
<span class="comment">// Wait for the task we just spawned to complete.</span>
|
||
<span class="ident">pool</span>.<span class="ident">shutdown_on_idle</span>().<span class="ident">wait</span>().<span class="ident">unwrap</span>();
|
||
}</pre>
|
||
</div></section>
|
||
<section id='search' class="content hidden"></section>
|
||
|
||
<section class="footer"></section>
|
||
|
||
<aside id="help" class="hidden">
|
||
<div>
|
||
<h1 class="hidden">Help</h1>
|
||
|
||
<div class="shortcuts">
|
||
<h2>Keyboard Shortcuts</h2>
|
||
|
||
<dl>
|
||
<dt><kbd>?</kbd></dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt><kbd>S</kbd></dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt><kbd>↑</kbd></dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt><kbd>↓</kbd></dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt><kbd>↹</kbd></dt>
|
||
<dd>Switch tab</dd>
|
||
<dt><kbd>⏎</kbd></dt>
|
||
<dd>Go to active search result</dd>
|
||
<dt><kbd>+</kbd></dt>
|
||
<dd>Expand all sections</dd>
|
||
<dt><kbd>-</kbd></dt>
|
||
<dd>Collapse all sections</dd>
|
||
</dl>
|
||
</div>
|
||
|
||
<div class="infos">
|
||
<h2>Search Tricks</h2>
|
||
|
||
<p>
|
||
Prefix searches with a type followed by a colon (e.g.
|
||
<code>fn:</code>) to restrict the search to a given type.
|
||
</p>
|
||
|
||
<p>
|
||
Accepted types are: <code>fn</code>, <code>mod</code>,
|
||
<code>struct</code>, <code>enum</code>,
|
||
<code>trait</code>, <code>type</code>, <code>macro</code>,
|
||
and <code>const</code>.
|
||
</p>
|
||
|
||
<p>
|
||
Search functions by type signature (e.g.
|
||
<code>vec -> usize</code> or <code>* -> vec</code>)
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../";
|
||
window.currentCrate = "tokio_threadpool";
|
||
</script>
|
||
<script src="../main.js"></script>
|
||
<script defer src="../search-index.js"></script>
|
||
</body>
|
||
</html> |