147 lines
18 KiB
HTML
147 lines
18 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 `runtime` mod in crate `tokio`."><meta name="keywords" content="rust, rustlang, rust-lang, runtime"><title>tokio::runtime - 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="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings"></script><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg">
|
||
<link rel="alternate icon" type="image/png" href="../../favicon-16x16.png">
|
||
<link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[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><a href='../../tokio/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a><p class="location">Module runtime</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li></ul></div><p class="location"><a href="../index.html">tokio</a></p><script>window.sidebarCurrent = {name: "runtime", ty: "mod", relpath: "../"};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><span class="help-button">?</span>
|
||
<a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><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/runtime/mod.rs.html#1-563" title="goto source code">[src]</a></span><span class="in-band">Module <a href="../index.html">tokio</a>::<wbr><a class="mod" href="">runtime</a></span></h1><div class="docblock"><p>The Tokio runtime.</p>
|
||
<p>Unlike other Rust programs, asynchronous applications require runtime
|
||
support. In particular, the following runtime services are necessary:</p>
|
||
<ul>
|
||
<li>An <strong>I/O event loop</strong>, called the driver, which drives I/O resources and
|
||
dispatches I/O events to tasks that depend on them.</li>
|
||
<li>A <strong>scheduler</strong> to execute <a href="../../tokio/task/index.html">tasks</a> that use these I/O resources.</li>
|
||
<li>A <strong>timer</strong> for scheduling work to run after a set period of time.</li>
|
||
</ul>
|
||
<p>Tokio's <a href="../../tokio/runtime/struct.Runtime.html"><code>Runtime</code></a> bundles all of these services as a single type, allowing
|
||
them to be started, shut down, and configured together. However, often it is
|
||
not required to configure a <a href="../../tokio/runtime/struct.Runtime.html"><code>Runtime</code></a> manually, and user may just use the
|
||
<a href="../attr.main.html"><code>tokio::main</code></a> attribute macro, which creates a <a href="../../tokio/runtime/struct.Runtime.html"><code>Runtime</code></a> under the hood.</p>
|
||
<h1 id="usage" class="section-header"><a href="#usage">Usage</a></h1>
|
||
<p>When no fine tuning is required, the <a href="../attr.main.html"><code>tokio::main</code></a> attribute macro can be
|
||
used.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">net</span>::<span class="ident">TcpListener</span>;
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">io</span>::{<span class="ident">AsyncReadExt</span>, <span class="ident">AsyncWriteExt</span>};
|
||
|
||
<span class="attribute">#[<span class="ident">tokio</span>::<span class="ident">main</span>]</span>
|
||
<span class="ident">async</span> <span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">></span> <span class="prelude-ty">Result</span><span class="op"><</span>(), <span class="ident">Box</span><span class="op"><</span><span class="ident">dyn</span> <span class="ident">std</span>::<span class="ident">error</span>::<span class="ident">Error</span><span class="op">></span><span class="op">></span> {
|
||
<span class="kw">let</span> <span class="ident">listener</span> <span class="op">=</span> <span class="ident">TcpListener</span>::<span class="ident">bind</span>(<span class="string">"127.0.0.1:8080"</span>).<span class="ident">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">loop</span> {
|
||
<span class="kw">let</span> (<span class="kw-2">mut</span> <span class="ident">socket</span>, <span class="kw">_</span>) <span class="op">=</span> <span class="ident">listener</span>.<span class="ident">accept</span>().<span class="ident">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="ident">tokio</span>::<span class="ident">spawn</span>(<span class="ident">async</span> <span class="kw">move</span> {
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">1024</span>];
|
||
|
||
<span class="comment">// In a loop, read data from the socket and write the data back.</span>
|
||
<span class="kw">loop</span> {
|
||
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">socket</span>.<span class="ident">read</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">buf</span>).<span class="ident">await</span> {
|
||
<span class="comment">// socket closed</span>
|
||
<span class="prelude-val">Ok</span>(<span class="ident">n</span>) <span class="kw">if</span> <span class="ident">n</span> <span class="op">=</span><span class="op">=</span> <span class="number">0</span> <span class="op">=</span><span class="op">></span> <span class="kw">return</span>,
|
||
<span class="prelude-val">Ok</span>(<span class="ident">n</span>) <span class="op">=</span><span class="op">></span> <span class="ident">n</span>,
|
||
<span class="prelude-val">Err</span>(<span class="ident">e</span>) <span class="op">=</span><span class="op">></span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"failed to read from socket; err = {:?}"</span>, <span class="ident">e</span>);
|
||
<span class="kw">return</span>;
|
||
}
|
||
};
|
||
|
||
<span class="comment">// Write the data back</span>
|
||
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="ident">e</span>) <span class="op">=</span> <span class="ident">socket</span>.<span class="ident">write_all</span>(<span class="kw-2">&</span><span class="ident">buf</span>[<span class="number">0</span>..<span class="ident">n</span>]).<span class="ident">await</span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"failed to write to socket; err = {:?}"</span>, <span class="ident">e</span>);
|
||
<span class="kw">return</span>;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
}</pre></div>
|
||
<p>From within the context of the runtime, additional tasks are spawned using
|
||
the <a href="../../tokio/fn.spawn.html"><code>tokio::spawn</code></a> function. Futures spawned using this function will be
|
||
executed on the same thread pool used by the <a href="../../tokio/runtime/struct.Runtime.html"><code>Runtime</code></a>.</p>
|
||
<p>A <a href="../../tokio/runtime/struct.Runtime.html"><code>Runtime</code></a> instance can also be used directly.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">net</span>::<span class="ident">TcpListener</span>;
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">io</span>::{<span class="ident">AsyncReadExt</span>, <span class="ident">AsyncWriteExt</span>};
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">runtime</span>::<span class="ident">Runtime</span>;
|
||
|
||
<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">></span> <span class="prelude-ty">Result</span><span class="op"><</span>(), <span class="ident">Box</span><span class="op"><</span><span class="ident">dyn</span> <span class="ident">std</span>::<span class="ident">error</span>::<span class="ident">Error</span><span class="op">></span><span class="op">></span> {
|
||
<span class="comment">// Create the runtime</span>
|
||
<span class="kw">let</span> <span class="ident">rt</span> <span class="op">=</span> <span class="ident">Runtime</span>::<span class="ident">new</span>()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Spawn the root task</span>
|
||
<span class="ident">rt</span>.<span class="ident">block_on</span>(<span class="ident">async</span> {
|
||
<span class="kw">let</span> <span class="ident">listener</span> <span class="op">=</span> <span class="ident">TcpListener</span>::<span class="ident">bind</span>(<span class="string">"127.0.0.1:8080"</span>).<span class="ident">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">loop</span> {
|
||
<span class="kw">let</span> (<span class="kw-2">mut</span> <span class="ident">socket</span>, <span class="kw">_</span>) <span class="op">=</span> <span class="ident">listener</span>.<span class="ident">accept</span>().<span class="ident">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="ident">tokio</span>::<span class="ident">spawn</span>(<span class="ident">async</span> <span class="kw">move</span> {
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">1024</span>];
|
||
|
||
<span class="comment">// In a loop, read data from the socket and write the data back.</span>
|
||
<span class="kw">loop</span> {
|
||
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">socket</span>.<span class="ident">read</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">buf</span>).<span class="ident">await</span> {
|
||
<span class="comment">// socket closed</span>
|
||
<span class="prelude-val">Ok</span>(<span class="ident">n</span>) <span class="kw">if</span> <span class="ident">n</span> <span class="op">=</span><span class="op">=</span> <span class="number">0</span> <span class="op">=</span><span class="op">></span> <span class="kw">return</span>,
|
||
<span class="prelude-val">Ok</span>(<span class="ident">n</span>) <span class="op">=</span><span class="op">></span> <span class="ident">n</span>,
|
||
<span class="prelude-val">Err</span>(<span class="ident">e</span>) <span class="op">=</span><span class="op">></span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"failed to read from socket; err = {:?}"</span>, <span class="ident">e</span>);
|
||
<span class="kw">return</span>;
|
||
}
|
||
};
|
||
|
||
<span class="comment">// Write the data back</span>
|
||
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="ident">e</span>) <span class="op">=</span> <span class="ident">socket</span>.<span class="ident">write_all</span>(<span class="kw-2">&</span><span class="ident">buf</span>[<span class="number">0</span>..<span class="ident">n</span>]).<span class="ident">await</span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"failed to write to socket; err = {:?}"</span>, <span class="ident">e</span>);
|
||
<span class="kw">return</span>;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
})
|
||
}</pre></div>
|
||
<h2 id="runtime-configurations" class="section-header"><a href="#runtime-configurations">Runtime Configurations</a></h2>
|
||
<p>Tokio provides multiple task scheduling strategies, suitable for different
|
||
applications. The <a href="../../tokio/runtime/struct.Builder.html">runtime builder</a> or <code>#[tokio::main]</code> attribute may be
|
||
used to select which scheduler to use.</p>
|
||
<h4 id="multi-thread-scheduler" class="section-header"><a href="#multi-thread-scheduler">Multi-Thread Scheduler</a></h4>
|
||
<p>The multi-thread scheduler executes futures on a <em>thread pool</em>, using a
|
||
work-stealing strategy. By default, it will start a worker thread for each
|
||
CPU core available on the system. This tends to be the ideal configurations
|
||
for most applications. The multi-thread scheduler requires the <code>rt-multi-thread</code>
|
||
feature flag, and is selected by default:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">runtime</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">threaded_rt</span> <span class="op">=</span> <span class="ident">runtime</span>::<span class="ident">Runtime</span>::<span class="ident">new</span>()<span class="question-mark">?</span>;</pre></div>
|
||
<p>Most applications should use the multi-thread scheduler, except in some
|
||
niche use-cases, such as when running only a single thread is required.</p>
|
||
<h4 id="current-thread-scheduler" class="section-header"><a href="#current-thread-scheduler">Current-Thread Scheduler</a></h4>
|
||
<p>The current-thread scheduler provides a <em>single-threaded</em> future executor.
|
||
All tasks will be created and executed on the current thread. This requires
|
||
the <code>rt</code> feature flag.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">runtime</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">basic_rt</span> <span class="op">=</span> <span class="ident">runtime</span>::<span class="ident">Builder</span>::<span class="ident">new_current_thread</span>()
|
||
.<span class="ident">build</span>()<span class="question-mark">?</span>;</pre></div>
|
||
<h4 id="resource-drivers" class="section-header"><a href="#resource-drivers">Resource drivers</a></h4>
|
||
<p>When configuring a runtime by hand, no resource drivers are enabled by
|
||
default. In this case, attempting to use networking types or time types will
|
||
fail. In order to enable these types, the resource drivers must be enabled.
|
||
This is done with <a href="../../tokio/runtime/struct.Builder.html#method.enable_io"><code>Builder::enable_io</code></a> and <a href="../../tokio/runtime/struct.Builder.html#method.enable_time"><code>Builder::enable_time</code></a>. As a
|
||
shorthand, <a href="../../tokio/runtime/struct.Builder.html#method.enable_all"><code>Builder::enable_all</code></a> enables both resource drivers.</p>
|
||
<h2 id="lifetime-of-spawned-threads" class="section-header"><a href="#lifetime-of-spawned-threads">Lifetime of spawned threads</a></h2>
|
||
<p>The runtime may spawn threads depending on its configuration and usage. The
|
||
multi-thread scheduler spawns threads to schedule tasks and for <code>spawn_blocking</code>
|
||
calls.</p>
|
||
<p>While the <code>Runtime</code> is active, threads may shutdown after periods of being
|
||
idle. Once <code>Runtime</code> is dropped, all runtime threads are forcibly shutdown.
|
||
Any tasks that have not yet completed will be dropped.</p>
|
||
</div><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2>
|
||
<table><tr class="module-item"><td><a class="struct" href="struct.Builder.html" title="tokio::runtime::Builder struct">Builder</a></td><td class="docblock-short"><p>Builds Tokio Runtime with custom configuration values.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.EnterGuard.html" title="tokio::runtime::EnterGuard struct">EnterGuard</a></td><td class="docblock-short"><p>Runtime context guard.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Handle.html" title="tokio::runtime::Handle struct">Handle</a></td><td class="docblock-short"><p>Handle to the runtime.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Runtime.html" title="tokio::runtime::Runtime struct">Runtime</a></td><td class="docblock-short"><p>The Tokio runtime.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../";window.currentCrate = "tokio";</script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html> |