78 lines
14 KiB
HTML
78 lines
14 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="A multi-producer, single-consumer queue for sending values between asynchronous tasks."><title>tokio::sync::mpsc - 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 mod"><!--[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="#">Module mpsc</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="#">Module mpsc</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#disconnection" title="Disconnection">Disconnection</a></li><li><a href="#clean-shutdown" title="Clean Shutdown">Clean Shutdown</a></li><li><a href="#communicating-between-sync-and-async-code" title="Communicating between sync and async code">Communicating between sync and async code</a></li><li><a href="#multiple-runtimes" title="Multiple runtimes">Multiple runtimes</a></li><li><a href="#allocation-behavior" title="Allocation behavior">Allocation behavior</a></li></ul><h3><a href="#modules">Module Items</a></h3><ul class="block"><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"><h2><a href="../index.html">In tokio::<wbr>sync</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>::<wbr><a href="../index.html">sync</a></div><h1>Module <span>mpsc</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/sync/mpsc/mod.rs.html#1-144">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A multi-producer, single-consumer queue for sending values between
|
||
asynchronous tasks.</p>
|
||
<p>This module provides two variants of the channel: bounded and unbounded. The
|
||
bounded variant has a limit on the number of messages that the channel can
|
||
store, and if this limit is reached, trying to send another message will
|
||
wait until a message is received from the channel. An unbounded channel has
|
||
an infinite capacity, so the <code>send</code> method will always complete immediately.
|
||
This makes the <a href="struct.UnboundedSender.html" title="struct tokio::sync::mpsc::UnboundedSender"><code>UnboundedSender</code></a> usable from both synchronous and
|
||
asynchronous code.</p>
|
||
<p>Similar to the <code>mpsc</code> channels provided by <code>std</code>, the channel constructor
|
||
functions provide separate send and receive handles, <a href="struct.Sender.html" title="struct tokio::sync::mpsc::Sender"><code>Sender</code></a> and
|
||
<a href="struct.Receiver.html" title="struct tokio::sync::mpsc::Receiver"><code>Receiver</code></a> for the bounded channel, <a href="struct.UnboundedSender.html" title="struct tokio::sync::mpsc::UnboundedSender"><code>UnboundedSender</code></a> and
|
||
<a href="struct.UnboundedReceiver.html" title="struct tokio::sync::mpsc::UnboundedReceiver"><code>UnboundedReceiver</code></a> for the unbounded channel. If there is no message to read,
|
||
the current task will be notified when a new value is sent. <a href="struct.Sender.html" title="struct tokio::sync::mpsc::Sender"><code>Sender</code></a> and
|
||
<a href="struct.UnboundedSender.html" title="struct tokio::sync::mpsc::UnboundedSender"><code>UnboundedSender</code></a> allow sending values into the channel. If the bounded
|
||
channel is at capacity, the send is rejected and the task will be notified
|
||
when additional capacity is available. In other words, the channel provides
|
||
backpressure.</p>
|
||
<p>This channel is also suitable for the single-producer single-consumer
|
||
use-case. (Unless you only need to send one message, in which case you
|
||
should use the <a href="../oneshot/index.html" title="mod tokio::sync::oneshot">oneshot</a> channel.)</p>
|
||
<h2 id="disconnection"><a class="doc-anchor" href="#disconnection">§</a>Disconnection</h2>
|
||
<p>When all <a href="struct.Sender.html" title="struct tokio::sync::mpsc::Sender"><code>Sender</code></a> handles have been dropped, it is no longer
|
||
possible to send values into the channel. This is considered the termination
|
||
event of the stream. As such, <code>Receiver::poll</code> returns <code>Ok(Ready(None))</code>.</p>
|
||
<p>If the <a href="struct.Receiver.html" title="struct tokio::sync::mpsc::Receiver"><code>Receiver</code></a> handle is dropped, then messages can no longer
|
||
be read out of the channel. In this case, all further attempts to send will
|
||
result in an error. Additionally, all unread messages will be drained from the
|
||
channel and dropped.</p>
|
||
<h2 id="clean-shutdown"><a class="doc-anchor" href="#clean-shutdown">§</a>Clean Shutdown</h2>
|
||
<p>When the <a href="struct.Receiver.html" title="struct tokio::sync::mpsc::Receiver"><code>Receiver</code></a> is dropped, it is possible for unprocessed messages to
|
||
remain in the channel. Instead, it is usually desirable to perform a “clean”
|
||
shutdown. To do this, the receiver first calls <code>close</code>, which will prevent
|
||
any further messages to be sent into the channel. Then, the receiver
|
||
consumes the channel to completion, at which point the receiver can be
|
||
dropped.</p>
|
||
<h2 id="communicating-between-sync-and-async-code"><a class="doc-anchor" href="#communicating-between-sync-and-async-code">§</a>Communicating between sync and async code</h2>
|
||
<p>When you want to communicate between synchronous and asynchronous code, there
|
||
are two situations to consider:</p>
|
||
<p><strong>Bounded channel</strong>: If you need a bounded channel, you should use a bounded
|
||
Tokio <code>mpsc</code> channel for both directions of communication. Instead of calling
|
||
the async <a href="struct.Sender.html#method.send" title="method tokio::sync::mpsc::Sender::send"><code>send</code></a> or <a href="struct.Receiver.html#method.recv" title="method tokio::sync::mpsc::Receiver::recv"><code>recv</code></a> methods, in
|
||
synchronous code you will need to use the <a href="struct.Sender.html#method.blocking_send" title="method tokio::sync::mpsc::Sender::blocking_send"><code>blocking_send</code></a> or
|
||
<a href="struct.Receiver.html#method.blocking_recv" title="method tokio::sync::mpsc::Receiver::blocking_recv"><code>blocking_recv</code></a> methods.</p>
|
||
<p><strong>Unbounded channel</strong>: You should use the kind of channel that matches where
|
||
the receiver is. So for sending a message <em>from async to sync</em>, you should
|
||
use <a href="https://doc.rust-lang.org/1.93.1/std/sync/mpsc/fn.channel.html" title="fn std::sync::mpsc::channel">the standard library unbounded channel</a> or
|
||
<a href="https://docs.rs/crossbeam/*/crossbeam/channel/fn.unbounded.html">crossbeam</a>. Similarly, for sending a message <em>from sync
|
||
to async</em>, you should use an unbounded Tokio <code>mpsc</code> channel.</p>
|
||
<p>Please be aware that the above remarks were written with the <code>mpsc</code> channel
|
||
in mind, but they can also be generalized to other kinds of channels. In
|
||
general, any channel method that isn’t marked async can be called anywhere,
|
||
including outside of the runtime. For example, sending a message on a
|
||
<a href="../oneshot/index.html" title="mod tokio::sync::oneshot">oneshot</a> channel from outside the runtime is perfectly fine.</p>
|
||
<h2 id="multiple-runtimes"><a class="doc-anchor" href="#multiple-runtimes">§</a>Multiple runtimes</h2>
|
||
<p>The <code>mpsc</code> channel is runtime agnostic. You can freely move it between
|
||
different instances of the Tokio runtime or even use it from non-Tokio
|
||
runtimes.</p>
|
||
<p>When used in a Tokio runtime, it participates in
|
||
<a href="../../task/coop/index.html#cooperative-scheduling" title="mod tokio::task::coop">cooperative scheduling</a> to avoid
|
||
starvation. This feature does not apply when used from non-Tokio runtimes.</p>
|
||
<p>As an exception, methods ending in <code>_timeout</code> are not runtime agnostic
|
||
because they require access to the Tokio timer. See the documentation of
|
||
each <code>*_timeout</code> method for more information on its use.</p>
|
||
<h2 id="allocation-behavior"><a class="doc-anchor" href="#allocation-behavior">§</a>Allocation behavior</h2><div class="warning">The implementation details described in this section may change in future
|
||
Tokio releases.</div>
|
||
<p>The mpsc channel stores elements in blocks. Blocks are organized in a linked list. Sending
|
||
pushes new elements onto the block at the front of the list, and receiving pops them off the
|
||
one at the back. A block can hold 32 messages on a 64-bit target and 16 messages on a 32-bit
|
||
target. This number is independent of channel and message size. Each block also stores 4
|
||
pointer-sized values for bookkeeping (so on a 64-bit machine, each message has 1 byte of
|
||
overhead).</p>
|
||
<p>When all values in a block have been received, it becomes empty. It will then be freed, unless
|
||
the channel’s first block (where newly-sent elements are being stored) has no next block. In
|
||
that case, the empty block is reused as the next block.</p>
|
||
</div></details><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="error/index.html" title="mod tokio::sync::mpsc::error">error</a></dt><dd>Channel error types.</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.OwnedPermit.html" title="struct tokio::sync::mpsc::OwnedPermit">Owned<wbr>Permit</a></dt><dd>Owned permit to send one value into the channel.</dd><dt><a class="struct" href="struct.Permit.html" title="struct tokio::sync::mpsc::Permit">Permit</a></dt><dd>Permits to send one value into the channel.</dd><dt><a class="struct" href="struct.PermitIterator.html" title="struct tokio::sync::mpsc::PermitIterator">Permit<wbr>Iterator</a></dt><dd>An <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator"><code>Iterator</code></a> of <a href="struct.Permit.html" title="struct tokio::sync::mpsc::Permit"><code>Permit</code></a> that can be used to hold <code>n</code> slots in the channel.</dd><dt><a class="struct" href="struct.Receiver.html" title="struct tokio::sync::mpsc::Receiver">Receiver</a></dt><dd>Receives values from the associated <code>Sender</code>.</dd><dt><a class="struct" href="struct.Sender.html" title="struct tokio::sync::mpsc::Sender">Sender</a></dt><dd>Sends values to the associated <code>Receiver</code>.</dd><dt><a class="struct" href="struct.UnboundedReceiver.html" title="struct tokio::sync::mpsc::UnboundedReceiver">Unbounded<wbr>Receiver</a></dt><dd>Receive values from the associated <code>UnboundedSender</code>.</dd><dt><a class="struct" href="struct.UnboundedSender.html" title="struct tokio::sync::mpsc::UnboundedSender">Unbounded<wbr>Sender</a></dt><dd>Send values to the associated <code>UnboundedReceiver</code>.</dd><dt><a class="struct" href="struct.WeakSender.html" title="struct tokio::sync::mpsc::WeakSender">Weak<wbr>Sender</a></dt><dd>A sender that does not prevent the channel from being closed.</dd><dt><a class="struct" href="struct.WeakUnboundedSender.html" title="struct tokio::sync::mpsc::WeakUnboundedSender">Weak<wbr>Unbounded<wbr>Sender</a></dt><dd>An unbounded sender that does not prevent the channel from being closed.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.channel.html" title="fn tokio::sync::mpsc::channel">channel</a></dt><dd>Creates a bounded mpsc channel for communicating between asynchronous tasks
|
||
with backpressure.</dd><dt><a class="fn" href="fn.unbounded_channel.html" title="fn tokio::sync::mpsc::unbounded_channel">unbounded_<wbr>channel</a></dt><dd>Creates an unbounded mpsc channel for communicating between asynchronous
|
||
tasks without backpressure.</dd></dl></section></div></main></body></html> |