Auto Generated Documentation
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
<!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 `Handle` struct in crate `tokio`."><meta name="keywords" content="rust, rustlang, rust-lang, Handle"><title>tokio::runtime::Handle - 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 struct"><!--[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">Struct Handle</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#implementations">Methods</a><div class="sidebar-links"><a href="#method.current">current</a><a href="#method.enter">enter</a><a href="#method.spawn">spawn</a><a href="#method.spawn_blocking">spawn_blocking</a><a href="#method.try_current">try_current</a></div><a class="sidebar-title" href="#trait-implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-Clone">Clone</a><a href="#impl-Debug">Debug</a></div><a class="sidebar-title" href="#synthetic-implementations">Auto Trait Implementations</a><div class="sidebar-links"><a href="#impl-RefUnwindSafe">!RefUnwindSafe</a><a href="#impl-Send">Send</a><a href="#impl-Sync">Sync</a><a href="#impl-Unpin">Unpin</a><a href="#impl-UnwindSafe">!UnwindSafe</a></div><a class="sidebar-title" href="#blanket-implementations">Blanket Implementations</a><div class="sidebar-links"><a href="#impl-Any">Any</a><a href="#impl-Borrow%3CT%3E">Borrow<T></a><a href="#impl-BorrowMut%3CT%3E">BorrowMut<T></a><a href="#impl-From%3CT%3E">From<T></a><a href="#impl-Into%3CU%3E">Into<U></a><a href="#impl-ToOwned">ToOwned</a><a href="#impl-TryFrom%3CU%3E">TryFrom<U></a><a href="#impl-TryInto%3CU%3E">TryInto<U></a></div></div><p class="location"><a href="../index.html">tokio</a>::<wbr><a href="index.html">runtime</a></p><script>window.sidebarCurrent = {name: "Handle", ty: "struct", 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><button type="button" class="help-button">?</button>
|
||||
<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 struct"><!--[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">Struct Handle</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#implementations">Methods</a><div class="sidebar-links"><a href="#method.block_on">block_on</a><a href="#method.current">current</a><a href="#method.enter">enter</a><a href="#method.spawn">spawn</a><a href="#method.spawn_blocking">spawn_blocking</a><a href="#method.try_current">try_current</a></div><a class="sidebar-title" href="#trait-implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-Clone">Clone</a><a href="#impl-Debug">Debug</a></div><a class="sidebar-title" href="#synthetic-implementations">Auto Trait Implementations</a><div class="sidebar-links"><a href="#impl-RefUnwindSafe">!RefUnwindSafe</a><a href="#impl-Send">Send</a><a href="#impl-Sync">Sync</a><a href="#impl-Unpin">Unpin</a><a href="#impl-UnwindSafe">!UnwindSafe</a></div><a class="sidebar-title" href="#blanket-implementations">Blanket Implementations</a><div class="sidebar-links"><a href="#impl-Any">Any</a><a href="#impl-Borrow%3CT%3E">Borrow<T></a><a href="#impl-BorrowMut%3CT%3E">BorrowMut<T></a><a href="#impl-From%3CT%3E">From<T></a><a href="#impl-Into%3CU%3E">Into<U></a><a href="#impl-ToOwned">ToOwned</a><a href="#impl-TryFrom%3CU%3E">TryFrom<U></a><a href="#impl-TryInto%3CU%3E">TryInto<U></a></div></div><p class="location"><a href="../index.html">tokio</a>::<wbr><a href="index.html">runtime</a></p><script>window.sidebarCurrent = {name: "Handle", ty: "struct", 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><button type="button" class="help-button">?</button>
|
||||
<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/handle.rs.html#16-33" title="goto source code">[src]</a></span><span class="in-band">Struct <a href="../index.html">tokio</a>::<wbr><a href="index.html">runtime</a>::<wbr><a class="struct" href="">Handle</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class="rust struct">pub struct Handle { /* fields omitted */ }</pre></div><div class="docblock"><p>Handle to the runtime.</p>
|
||||
<p>The handle is internally reference-counted and can be freely cloned. A handle can be
|
||||
obtained using the <a href="../../tokio/runtime/struct.Runtime.html#method.handle"><code>Runtime::handle</code></a> method.</p>
|
||||
</div><h2 id="implementations" class="small-section-header">Implementations<a href="#implementations" class="anchor"></a></h2><h3 id="impl" class="impl"><code class="in-band">impl <a class="struct" href="../../tokio/runtime/struct.Handle.html" title="struct tokio::runtime::Handle">Handle</a></code><a href="#impl" class="anchor"></a><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#47-204" title="goto source code">[src]</a></h3><div class="impl-items"><h4 id="method.enter" class="method"><code>pub fn <a href="#method.enter" class="fnname">enter</a>(&self) -> <a class="struct" href="../../tokio/runtime/struct.EnterGuard.html" title="struct tokio::runtime::EnterGuard">EnterGuard</a><'_></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#55-60" title="goto source code">[src]</a></h4><div class="docblock"><p>Enter the runtime context. This allows you to construct types that must
|
||||
</div><h2 id="implementations" class="small-section-header">Implementations<a href="#implementations" class="anchor"></a></h2><h3 id="impl" class="impl"><code class="in-band">impl <a class="struct" href="../../tokio/runtime/struct.Handle.html" title="struct tokio::runtime::Handle">Handle</a></code><a href="#impl" class="anchor"></a><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#48-292" title="goto source code">[src]</a></h3><div class="impl-items"><h4 id="method.enter" class="method"><code>pub fn <a href="#method.enter" class="fnname">enter</a>(&self) -> <a class="struct" href="../../tokio/runtime/struct.EnterGuard.html" title="struct tokio::runtime::EnterGuard">EnterGuard</a><'_></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#56-61" title="goto source code">[src]</a></h4><div class="docblock"><p>Enter the runtime context. This allows you to construct types that must
|
||||
have an executor available on creation such as <a href="../../tokio/time/struct.Sleep.html"><code>Sleep</code></a> or <a href="../../tokio/net/struct.TcpStream.html"><code>TcpStream</code></a>.
|
||||
It will also allow you to call methods such as <a href="../../tokio/task/fn.spawn.html"><code>tokio::spawn</code></a>.</p>
|
||||
</div><h4 id="method.current" class="method"><code>pub fn <a href="#method.current" class="fnname">current</a>() -> Self</code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#100-102" title="goto source code">[src]</a></h4><div class="docblock"><p>Returns a <code>Handle</code> view over the currently running <code>Runtime</code></p>
|
||||
</div><h4 id="method.current" class="method"><code>pub fn <a href="#method.current" class="fnname">current</a>() -> Self</code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#101-103" title="goto source code">[src]</a></h4><div class="docblock"><p>Returns a <code>Handle</code> view over the currently running <code>Runtime</code></p>
|
||||
<h1 id="panic" class="section-header"><a href="#panic">Panic</a></h1>
|
||||
<p>This will panic if called outside the context of a Tokio runtime. That means that you must
|
||||
call this on one of the threads <strong>being run by the runtime</strong>. Calling this from within a
|
||||
@@ -31,10 +31,10 @@ block or function running on that runtime.</p>
|
||||
<span class="comment">// This next line would cause a panic</span>
|
||||
<span class="comment">// let handle2 = Handle::current();</span>
|
||||
});</pre></div>
|
||||
</div><h4 id="method.try_current" class="method"><code>pub fn <a href="#method.try_current" class="fnname">try_current</a>() -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self, TryCurrentError></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#109-111" title="goto source code">[src]</a></h4><div class="docblock"><p>Returns a Handle view over the currently running Runtime</p>
|
||||
</div><h4 id="method.try_current" class="method"><code>pub fn <a href="#method.try_current" class="fnname">try_current</a>() -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self, TryCurrentError></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#110-112" title="goto source code">[src]</a></h4><div class="docblock"><p>Returns a Handle view over the currently running Runtime</p>
|
||||
<p>Returns an error if no Runtime has been started</p>
|
||||
<p>Contrary to <code>current</code>, this never panics</p>
|
||||
</div><h4 id="method.spawn" class="method"><code>pub fn <a href="#method.spawn" class="fnname">spawn</a><F>(&self, future: F) -> <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><F::<a class="type" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><h3 class="notable">Notable traits for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></h3><code class="content"><span class="where fmt-newline">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="type">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="../../tokio/task/struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>;</span></code></span></div></span></span> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br> F::<a class="type" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static, </span></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#141-149" title="goto source code">[src]</a></h4><div class="docblock"><p>Spawn a future onto the Tokio runtime.</p>
|
||||
</div><h4 id="method.spawn" class="method"><code>pub fn <a href="#method.spawn" class="fnname">spawn</a><F>(&self, future: F) -> <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><F::<a class="type" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><h3 class="notable">Notable traits for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></h3><code class="content"><span class="where fmt-newline">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="type">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="../../tokio/task/struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>;</span></code></span></div></span></span> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br> F::<a class="type" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static, </span></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#142-150" title="goto source code">[src]</a></h4><div class="docblock"><p>Spawn a future onto the Tokio runtime.</p>
|
||||
<p>This spawns the given future onto the runtime's executor, usually a
|
||||
thread pool. The thread pool is then responsible for polling the future
|
||||
until it completes.</p>
|
||||
@@ -52,7 +52,7 @@ until it completes.</p>
|
||||
<span class="ident">handle</span>.<span class="ident">spawn</span>(<span class="kw">async</span> {
|
||||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"now running on a worker thread"</span>);
|
||||
});</pre></div>
|
||||
</div><h4 id="method.spawn_blocking" class="method"><code>pub fn <a href="#method.spawn_blocking" class="fnname">spawn_blocking</a><F, R>(&self, func: F) -> <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><R><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><h3 class="notable">Notable traits for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></h3><code class="content"><span class="where fmt-newline">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="type">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="../../tokio/task/struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>;</span></code></span></div></span></span> <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>() -> R + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br> R: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static, </span></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#171-203" title="goto source code">[src]</a></h4><div class="docblock"><p>Run the provided function on an executor dedicated to blocking
|
||||
</div><h4 id="method.spawn_blocking" class="method"><code>pub fn <a href="#method.spawn_blocking" class="fnname">spawn_blocking</a><F, R>(&self, func: F) -> <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><R><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><h3 class="notable">Notable traits for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></h3><code class="content"><span class="where fmt-newline">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="../../tokio/task/struct.JoinHandle.html" title="struct tokio::task::JoinHandle">JoinHandle</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="type">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="../../tokio/task/struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>;</span></code></span></div></span></span> <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>() -> R + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br> R: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static, </span></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#172-204" title="goto source code">[src]</a></h4><div class="docblock"><p>Run the provided function on an executor dedicated to blocking
|
||||
operations.</p>
|
||||
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||||
@@ -67,6 +67,55 @@ operations.</p>
|
||||
<span class="ident">handle</span>.<span class="ident">spawn_blocking</span>(<span class="op">|</span><span class="op">|</span> {
|
||||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"now running on a worker thread"</span>);
|
||||
});</pre></div>
|
||||
</div><h4 id="method.block_on" class="method"><code>pub fn <a href="#method.block_on" class="fnname">block_on</a><F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>>(&self, future: F) -> F::<a class="type" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#276-287" title="goto source code">[src]</a></h4><div class="docblock"><p>Run a future to completion on this <code>Handle</code>'s associated <code>Runtime</code>.</p>
|
||||
<p>This runs the given future on the runtime, blocking until it is
|
||||
complete, and yielding its resolved result. Any tasks or timers which
|
||||
the future spawns internally will be executed on the runtime.</p>
|
||||
<p>When this is used on a <code>current_thread</code> runtime, only the
|
||||
<a href="../../tokio/runtime/struct.Runtime.html#method.block_on"><code>Runtime::block_on</code></a> method can drive the IO and timer drivers, but the
|
||||
<code>Handle::block_on</code> method cannot drive them. This means that, when using
|
||||
this method on a current_thread runtime, anything that relies on IO or
|
||||
timers will not work unless there is another thread currently calling
|
||||
<a href="../../tokio/runtime/struct.Runtime.html#method.block_on"><code>Runtime::block_on</code></a> on the same runtime.</p>
|
||||
<h1 id="if-the-runtime-has-been-shut-down" class="section-header"><a href="#if-the-runtime-has-been-shut-down">If the runtime has been shut down</a></h1>
|
||||
<p>If the <code>Handle</code>'s associated <code>Runtime</code> has been shut down (through
|
||||
<a href="../../tokio/runtime/struct.Runtime.html#method.shutdown_background"><code>Runtime::shutdown_background</code></a>, <a href="../../tokio/runtime/struct.Runtime.html#method.shutdown_timeout"><code>Runtime::shutdown_timeout</code></a>, or by
|
||||
dropping it) and <code>Handle::block_on</code> is used it might return an error or
|
||||
panic. Specifically IO resources will return an error and timers will
|
||||
panic. Runtime independent futures will run as normal.</p>
|
||||
<h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1>
|
||||
<p>This function panics if the provided future panics, if called within an
|
||||
asynchronous execution context, or if a timer future is executed on a
|
||||
runtime that has been shut down.</p>
|
||||
<h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1>
|
||||
<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="ident">Runtime</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="ident">unwrap</span>();
|
||||
|
||||
<span class="comment">// Get a handle from this runtime</span>
|
||||
<span class="kw">let</span> <span class="ident">handle</span> <span class="op">=</span> <span class="ident">rt</span>.<span class="ident">handle</span>();
|
||||
|
||||
<span class="comment">// Execute the future, blocking the current thread until completion</span>
|
||||
<span class="ident">handle</span>.<span class="ident">block_on</span>(<span class="kw">async</span> {
|
||||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"hello"</span>);
|
||||
});</pre></div>
|
||||
<p>Or using <code>Handle::current</code>:</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="ident">Handle</span>;
|
||||
|
||||
<span class="attribute">#[<span class="ident">tokio</span>::<span class="ident">main</span>]</span>
|
||||
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">main</span> () {
|
||||
<span class="kw">let</span> <span class="ident">handle</span> <span class="op">=</span> <span class="ident">Handle</span>::<span class="ident">current</span>();
|
||||
<span class="ident">std</span>::<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> {
|
||||
<span class="comment">// Using Handle::block_on to run async code in the new thread.</span>
|
||||
<span class="ident">handle</span>.<span class="ident">block_on</span>(<span class="kw">async</span> {
|
||||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"hello"</span>);
|
||||
});
|
||||
});
|
||||
}</pre></div>
|
||||
</div></div><h2 id="trait-implementations" class="small-section-header">Trait Implementations<a href="#trait-implementations" class="anchor"></a></h2><div id="trait-implementations-list"><h3 id="impl-Clone" class="impl"><code class="in-band">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> for <a class="struct" href="../../tokio/runtime/struct.Handle.html" title="struct tokio::runtime::Handle">Handle</a></code><a href="#impl-Clone" class="anchor"></a><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#15" title="goto source code">[src]</a></h3><div class="impl-items"><h4 id="method.clone" class="method hidden"><code>fn <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html#tymethod.clone" class="fnname">clone</a>(&self) -> <a class="struct" href="../../tokio/runtime/struct.Handle.html" title="struct tokio::runtime::Handle">Handle</a></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#15" title="goto source code">[src]</a></h4><div class='docblock hidden'><p>Returns a copy of the value. <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html#tymethod.clone">Read more</a></p>
|
||||
</div><h4 id="method.clone_from" class="method hidden"><code>pub fn <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html#method.clone_from" class="fnname">clone_from</a>(&mut self, source: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>Self)</code><span class="since" title="Stable since Rust version 1.0.0">1.0.0</span><a class="srclink" href="https://doc.rust-lang.org/nightly/src/core/clone.rs.html#128-130" title="goto source code">[src]</a></h4><div class='docblock hidden'><p>Performs copy-assignment from <code>source</code>. <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html#method.clone_from">Read more</a></p>
|
||||
</div></div><h3 id="impl-Debug" class="impl"><code class="in-band">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="../../tokio/runtime/struct.Handle.html" title="struct tokio::runtime::Handle">Handle</a></code><a href="#impl-Debug" class="anchor"></a><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#15" title="goto source code">[src]</a></h3><div class="impl-items"><h4 id="method.fmt" class="method hidden"><code>fn <a href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#tymethod.fmt" class="fnname">fmt</a>(&self, f: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a><'_>) -> <a class="type" href="https://doc.rust-lang.org/nightly/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></code><a class="srclink" href="../../src/tokio/runtime/handle.rs.html#15" title="goto source code">[src]</a></h4><div class='docblock hidden'><p>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></p>
|
||||
|
||||
Reference in New Issue
Block a user