71 lines
12 KiB
HTML
71 lines
12 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="Utilities for tracking time."><title>tokio::time - 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 time</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 time</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#reexports">Module Items</a></h3><ul class="block"><li><a href="#reexports" title="Re-exports">Re-exports</a></li><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li><li><a href="#functions" title="Functions">Functions</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>Module <span>time</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/time/mod.rs.html#1-110">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Utilities for tracking time.</p>
|
|
<p>This module provides a number of types for executing code after a set period
|
|
of time.</p>
|
|
<ul>
|
|
<li>
|
|
<p><a href="fn.sleep.html" title="fn tokio::time::sleep"><code>Sleep</code></a> is a future that does no work and completes at a specific <a href="struct.Instant.html" title="struct tokio::time::Instant"><code>Instant</code></a>
|
|
in time.</p>
|
|
</li>
|
|
<li>
|
|
<p><a href="fn.interval.html" title="fn tokio::time::interval"><code>Interval</code></a> is a stream yielding a value at a fixed period. It is
|
|
initialized with a <a href="https://doc.rust-lang.org/1.93.1/core/time/struct.Duration.html" title="struct core::time::Duration"><code>Duration</code></a> and repeatedly yields each time the duration
|
|
elapses.</p>
|
|
</li>
|
|
<li>
|
|
<p><a href="struct.Timeout.html" title="struct tokio::time::Timeout"><code>Timeout</code></a>: Wraps a future or stream, setting an upper bound to the amount
|
|
of time it is allowed to execute. If the future or stream does not
|
|
complete in time, then it is canceled and an error is returned.</p>
|
|
</li>
|
|
</ul>
|
|
<p>These types are sufficient for handling a large number of scenarios
|
|
involving time.</p>
|
|
<p>These types must be used from within the context of the <a href="../runtime/struct.Runtime.html" title="struct tokio::runtime::Runtime"><code>Runtime</code></a>.</p>
|
|
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
|
<p>Wait 100ms and print “100 ms have elapsed”</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::time::Duration;
|
|
<span class="kw">use </span>tokio::time::sleep;
|
|
|
|
sleep(Duration::from_millis(<span class="number">100</span>)).<span class="kw">await</span>;
|
|
<span class="macro">println!</span>(<span class="string">"100 ms have elapsed"</span>);</code></pre></div>
|
|
<p>Require that an operation takes no more than 1s.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::time::{timeout, Duration};
|
|
|
|
<span class="kw">async fn </span>long_future() {
|
|
<span class="comment">// do work here
|
|
</span>}
|
|
|
|
<span class="kw">let </span>res = timeout(Duration::from_secs(<span class="number">1</span>), long_future()).<span class="kw">await</span>;
|
|
|
|
<span class="kw">if </span>res.is_err() {
|
|
<span class="macro">println!</span>(<span class="string">"operation timed out"</span>);
|
|
}</code></pre></div>
|
|
<p>A simple example using <a href="fn.interval.html" title="fn tokio::time::interval"><code>interval</code></a> to execute a task every two seconds.</p>
|
|
<p>The difference between <a href="fn.interval.html" title="fn tokio::time::interval"><code>interval</code></a> and <a href="fn.sleep.html" title="fn tokio::time::sleep"><code>sleep</code></a> is that an <a href="fn.interval.html" title="fn tokio::time::interval"><code>interval</code></a>
|
|
measures the time since the last tick, which means that <code>.tick().await</code> may
|
|
wait for a shorter time than the duration specified for the interval
|
|
if some time has passed between calls to <code>.tick().await</code>.</p>
|
|
<p>If the tick in the example below was replaced with <a href="fn.sleep.html" title="fn tokio::time::sleep"><code>sleep</code></a>, the task
|
|
would only be executed once every three seconds, and not every two
|
|
seconds.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::time;
|
|
|
|
<span class="kw">async fn </span>task_that_takes_a_second() {
|
|
<span class="macro">println!</span>(<span class="string">"hello"</span>);
|
|
time::sleep(time::Duration::from_secs(<span class="number">1</span>)).<span class="kw">await
|
|
</span>}
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>interval = time::interval(time::Duration::from_secs(<span class="number">2</span>));
|
|
<span class="kw">for </span>_i <span class="kw">in </span><span class="number">0</span>..<span class="number">5 </span>{
|
|
interval.tick().<span class="kw">await</span>;
|
|
task_that_takes_a_second().<span class="kw">await</span>;
|
|
}</code></pre></div></div></details><h2 id="reexports" class="section-header">Re-exports<a href="#reexports" class="anchor">§</a></h2><dl class="item-table reexports"><dt id="reexport.Duration"><code>pub use std::time::<a class="struct" href="https://doc.rust-lang.org/1.93.1/core/time/struct.Duration.html" title="struct core::time::Duration">Duration</a>;</code></dt></dl><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::time::error">error</a></dt><dd>Time 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.Instant.html" title="struct tokio::time::Instant">Instant</a></dt><dd>A measurement of a monotonically nondecreasing clock.
|
|
Opaque and useful only with <code>Duration</code>.</dd><dt><a class="struct" href="struct.Interval.html" title="struct tokio::time::Interval">Interval</a></dt><dd>Interval returned by <a href="fn.interval.html" title="fn tokio::time::interval"><code>interval</code></a> and <a href="fn.interval_at.html" title="fn tokio::time::interval_at"><code>interval_at</code></a>.</dd><dt><a class="struct" href="struct.Sleep.html" title="struct tokio::time::Sleep">Sleep</a></dt><dd>Future returned by <a href="fn.sleep.html" title="fn tokio::time::sleep"><code>sleep</code></a> and <a href="fn.sleep_until.html" title="fn tokio::time::sleep_until"><code>sleep_until</code></a>.</dd><dt><a class="struct" href="struct.Timeout.html" title="struct tokio::time::Timeout">Timeout</a></dt><dd>Future returned by <a href="fn.timeout.html" title="fn tokio::time::timeout"><code>timeout</code></a> and <a href="fn.timeout_at.html" title="fn tokio::time::timeout_at"><code>timeout_at</code></a>.</dd></dl><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><dl class="item-table"><dt><a class="enum" href="enum.MissedTickBehavior.html" title="enum tokio::time::MissedTickBehavior">Missed<wbr>Tick<wbr>Behavior</a></dt><dd>Defines the behavior of an <a href="struct.Interval.html" title="struct tokio::time::Interval"><code>Interval</code></a> when it misses a tick.</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.interval.html" title="fn tokio::time::interval">interval</a></dt><dd>Creates new <a href="struct.Interval.html" title="struct tokio::time::Interval"><code>Interval</code></a> that yields with interval of <code>period</code>. The first
|
|
tick completes immediately. The default <a href="enum.MissedTickBehavior.html" title="enum tokio::time::MissedTickBehavior"><code>MissedTickBehavior</code></a> is
|
|
<a href="enum.MissedTickBehavior.html#variant.Burst" title="variant tokio::time::MissedTickBehavior::Burst"><code>Burst</code></a>, but this can be configured
|
|
by calling <a href="struct.Interval.html#method.set_missed_tick_behavior" title="method tokio::time::Interval::set_missed_tick_behavior"><code>set_missed_tick_behavior</code></a>.</dd><dt><a class="fn" href="fn.interval_at.html" title="fn tokio::time::interval_at">interval_<wbr>at</a></dt><dd>Creates new <a href="struct.Interval.html" title="struct tokio::time::Interval"><code>Interval</code></a> that yields with interval of <code>period</code> with the
|
|
first tick completing at <code>start</code>. The default <a href="enum.MissedTickBehavior.html" title="enum tokio::time::MissedTickBehavior"><code>MissedTickBehavior</code></a> is
|
|
<a href="enum.MissedTickBehavior.html#variant.Burst" title="variant tokio::time::MissedTickBehavior::Burst"><code>Burst</code></a>, but this can be configured
|
|
by calling <a href="struct.Interval.html#method.set_missed_tick_behavior" title="method tokio::time::Interval::set_missed_tick_behavior"><code>set_missed_tick_behavior</code></a>.</dd><dt><a class="fn" href="fn.sleep.html" title="fn tokio::time::sleep">sleep</a></dt><dd>Waits until <code>duration</code> has elapsed.</dd><dt><a class="fn" href="fn.sleep_until.html" title="fn tokio::time::sleep_until">sleep_<wbr>until</a></dt><dd>Waits until <code>deadline</code> is reached.</dd><dt><a class="fn" href="fn.timeout.html" title="fn tokio::time::timeout">timeout</a></dt><dd>Requires a <code>Future</code> to complete before the specified duration has elapsed.</dd><dt><a class="fn" href="fn.timeout_at.html" title="fn tokio::time::timeout_at">timeout_<wbr>at</a></dt><dd>Requires a <code>Future</code> to complete before the specified instant in time.</dd></dl></section></div></main></body></html> |