112 lines
10 KiB
HTML
112 lines
10 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="Waits on multiple concurrent branches, returning when all branches complete with `Ok(_)` or on the first `Err(_)`."><title>try_join in tokio - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="tokio" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">try_join</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../tokio/index.html">tokio</a><span class="version">1.49.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">try_<wbr>join</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#notes" title="Notes">Notes</a><ul><li><a href="#runtime-characteristics" title="Runtime characteristics">Runtime characteristics</a></li><li><a href="#fairness" title="Fairness">Fairness</a></li></ul></li><li><a href="#examples" title="Examples">Examples</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate tokio</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">tokio</a></div><h1>Macro <span class="macro">try_<wbr>join</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/tokio/macros/try_join.rs.html#160-162">Source</a> </span></div><pre class="rust item-decl"><code>macro_rules! try_join {
|
||
($(biased;)? $($future:expr),*) => { ... };
|
||
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Waits on multiple concurrent branches, returning when <strong>all</strong> branches
|
||
complete with <code>Ok(_)</code> or on the first <code>Err(_)</code>.</p>
|
||
<p>The <code>try_join!</code> macro must be used inside of async functions, closures, and
|
||
blocks.</p>
|
||
<p>Similar to <a href="macro.join.html" title="macro tokio::join"><code>join!</code></a>, the <code>try_join!</code> macro takes a list of async
|
||
expressions and evaluates them concurrently on the same task. Each async
|
||
expression evaluates to a future and the futures from each expression are
|
||
multiplexed on the current task. The <code>try_join!</code> macro returns when <strong>all</strong>
|
||
branches return with <code>Ok</code> or when the <strong>first</strong> branch returns with <code>Err</code>.</p>
|
||
<h2 id="notes"><a class="doc-anchor" href="#notes">§</a>Notes</h2>
|
||
<p>The supplied futures are stored inline and do not require allocating a
|
||
<code>Vec</code>.</p>
|
||
<h3 id="runtime-characteristics"><a class="doc-anchor" href="#runtime-characteristics">§</a>Runtime characteristics</h3>
|
||
<p>By running all async expressions on the current task, the expressions are
|
||
able to run <strong>concurrently</strong> but not in <strong>parallel</strong>. This means all
|
||
expressions are run on the same thread and if one branch blocks the thread,
|
||
all other expressions will be unable to continue. If parallelism is
|
||
required, spawn each async expression using <a href="task/fn.spawn.html" title="fn tokio::task::spawn"><code>tokio::spawn</code></a> and pass the
|
||
join handle to <code>try_join!</code>.</p>
|
||
<h3 id="fairness"><a class="doc-anchor" href="#fairness">§</a>Fairness</h3>
|
||
<p>By default, <code>try_join!</code>’s generated future rotates which
|
||
contained future is polled first whenever it is woken.</p>
|
||
<p>This behavior can be overridden by adding <code>biased;</code> to the beginning of the
|
||
macro usage. See the examples for details. This will cause <code>try_join</code> to poll
|
||
the futures in the order they appear from top to bottom.</p>
|
||
<p>You may want this if your futures may interact in a way where known polling order is significant.</p>
|
||
<p>But there is an important caveat to this mode. It becomes your responsibility
|
||
to ensure that the polling order of your futures is fair. If for example you
|
||
are joining a stream and a shutdown future, and the stream has a
|
||
huge volume of messages that takes a long time to finish processing per poll, you should
|
||
place the shutdown future earlier in the <code>try_join!</code> list to ensure that it is
|
||
always polled, and will not be delayed due to the stream future taking a long time to return
|
||
<code>Poll::Pending</code>.</p>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<p>Basic <code>try_join</code> with two branches.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">async fn </span>do_stuff_async() -> <span class="prelude-ty">Result</span><(), <span class="kw-2">&</span><span class="lifetime">'static </span>str> {
|
||
<span class="comment">// async work
|
||
</span>}
|
||
|
||
<span class="kw">async fn </span>more_async_work() -> <span class="prelude-ty">Result</span><(), <span class="kw-2">&</span><span class="lifetime">'static </span>str> {
|
||
<span class="comment">// more here
|
||
</span>}
|
||
|
||
<span class="kw">let </span>res = <span class="macro">tokio::try_join!</span>(
|
||
do_stuff_async(),
|
||
more_async_work());
|
||
|
||
<span class="kw">match </span>res {
|
||
<span class="prelude-val">Ok</span>((first, second)) => {
|
||
<span class="comment">// do something with the values
|
||
</span>}
|
||
<span class="prelude-val">Err</span>(err) => {
|
||
<span class="macro">println!</span>(<span class="string">"processing failed; error = {}"</span>, err);
|
||
}
|
||
}</code></pre></div>
|
||
<p>Using <code>try_join!</code> with spawned tasks.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::task::JoinHandle;
|
||
|
||
<span class="kw">async fn </span>do_stuff_async() -> <span class="prelude-ty">Result</span><(), <span class="kw-2">&</span><span class="lifetime">'static </span>str> {
|
||
<span class="comment">// async work
|
||
</span>}
|
||
|
||
<span class="kw">async fn </span>more_async_work() -> <span class="prelude-ty">Result</span><(), <span class="kw-2">&</span><span class="lifetime">'static </span>str> {
|
||
<span class="comment">// more here
|
||
</span>}
|
||
|
||
<span class="kw">async fn </span>flatten<T>(handle: JoinHandle<<span class="prelude-ty">Result</span><T, <span class="kw-2">&</span><span class="lifetime">'static </span>str>>) -> <span class="prelude-ty">Result</span><T, <span class="kw-2">&</span><span class="lifetime">'static </span>str> {
|
||
<span class="kw">match </span>handle.<span class="kw">await </span>{
|
||
<span class="prelude-val">Ok</span>(<span class="prelude-val">Ok</span>(result)) => <span class="prelude-val">Ok</span>(result),
|
||
<span class="prelude-val">Ok</span>(<span class="prelude-val">Err</span>(err)) => <span class="prelude-val">Err</span>(err),
|
||
<span class="prelude-val">Err</span>(err) => <span class="prelude-val">Err</span>(<span class="string">"handling failed"</span>),
|
||
}
|
||
}
|
||
|
||
<span class="kw">let </span>handle1 = tokio::spawn(do_stuff_async());
|
||
<span class="kw">let </span>handle2 = tokio::spawn(more_async_work());
|
||
<span class="kw">match </span><span class="macro">tokio::try_join!</span>(flatten(handle1), flatten(handle2)) {
|
||
<span class="prelude-val">Ok</span>(val) => {
|
||
<span class="comment">// do something with the values
|
||
</span>}
|
||
<span class="prelude-val">Err</span>(err) => {
|
||
<span class="macro">println!</span>(<span class="string">"Failed with {}."</span>, err);
|
||
}
|
||
}</code></pre></div>
|
||
<p>Using the <code>biased;</code> mode to control polling order.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">async fn </span>do_stuff_async() -> <span class="prelude-ty">Result</span><(), <span class="kw-2">&</span><span class="lifetime">'static </span>str> {
|
||
<span class="comment">// async work
|
||
</span>}
|
||
|
||
<span class="kw">async fn </span>more_async_work() -> <span class="prelude-ty">Result</span><(), <span class="kw-2">&</span><span class="lifetime">'static </span>str> {
|
||
<span class="comment">// more here
|
||
</span>}
|
||
|
||
<span class="kw">let </span>res = <span class="macro">tokio::try_join!</span>(
|
||
biased;
|
||
do_stuff_async(),
|
||
more_async_work()
|
||
);
|
||
|
||
<span class="kw">match </span>res {
|
||
<span class="prelude-val">Ok</span>((first, second)) => {
|
||
<span class="comment">// do something with the values
|
||
</span>}
|
||
<span class="prelude-val">Err</span>(err) => {
|
||
<span class="macro">println!</span>(<span class="string">"processing failed; error = {}"</span>, err);
|
||
}
|
||
}</code></pre></div></div></details></section></div></main></body></html> |