{"id":640,"date":"2026-08-04T00:45:12","date_gmt":"2026-08-03T16:45:12","guid":{"rendered":"https:\/\/cococat.top\/?p=640"},"modified":"2026-08-04T00:45:12","modified_gmt":"2026-08-03T16:45:12","slug":"rust-%e7%ac%94%e8%ae%b0-11%ef%bc%9atokio-%e6%97%b6%e9%97%b4%e8%bd%ae","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2026\/08\/04\/rust-%e7%ac%94%e8%ae%b0-11%ef%bc%9atokio-%e6%97%b6%e9%97%b4%e8%bd%ae\/","title":{"rendered":"Rust \u7b14\u8bb0 11\uff1atokio \u65f6\u95f4\u8f6e"},"content":{"rendered":"<h2>1. \u65f6\u95f4\u8f6e\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898<\/h2>\n<p>\u65f6\u95f4\u8f6e\u7528\u4e8e\u9ad8\u6548\u7ba1\u7406\u5927\u91cf\u5b9a\u65f6\u4efb\u52a1\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u8bf7\u6c42\u8d85\u65f6<\/li>\n<li>\u5fc3\u8df3<\/li>\n<li>\u5ef6\u8fdf\u91cd\u8bd5<\/li>\n<li>tokio::time::sleep<\/li>\n<li>tokio::time::interval<\/li>\n<\/ul>\n<p>\u5b83\u7c7b\u4f3c\u949f\u8868\u8868\u76d8\uff1a<\/p>\n<ul>\n<li>\u8868\u76d8\u88ab\u5212\u5206\u6210\u591a\u4e2a\u65f6\u95f4\u69fd\u3002<\/li>\n<li>\u5b9a\u65f6\u4efb\u52a1\u6839\u636e deadline \u653e\u5165\u5bf9\u5e94\u69fd\u4f4d\u3002<\/li>\n<li>\u65f6\u95f4\u5411\u524d\u63a8\u8fdb\u65f6\uff0c\u5904\u7406\u5df2\u7ecf\u5230\u671f\u7684\u69fd\u4f4d\u3002<\/li>\n<\/ul>\n<p>Tokio \u4f7f\u7528\u5206\u5c42\u65f6\u95f4\u8f6e\uff0c\u7c7b\u4f3c\u6beb\u79d2\u3001\u79d2\u3001\u5206\u3001\u65f6\u591a\u7ea7\u8868\u76d8\uff1a<\/p>\n<ul>\n<li>\u4e34\u8fd1\u4efb\u52a1\u653e\u5728\u4f4e\u5c42\uff0c\u4fdd\u8bc1\u8f83\u9ad8\u7cbe\u5ea6\u3002<\/li>\n<li>\u957f\u671f\u4efb\u52a1\u653e\u5728\u9ad8\u5c42\u3002<\/li>\n<li>deadline \u63a5\u8fd1\u540e\uff0c\u4efb\u52a1\u9010\u7ea7\u4e0b\u6c89\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u7ed3\u6784\u63d2\u5165\u3001\u5220\u9664\u548c\u5230\u671f\u5904\u7406\u901a\u5e38\u63a5\u8fd1 O(1)\uff0c\u9002\u5408\u7ba1\u7406\u5927\u91cf\u5b9a\u65f6\u5668\u3002<\/p>\n<h2>2. \u65f6\u95f4\u8f6e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5206\u5de5<\/h2>\n<p>\u65f6\u95f4\u8f6e\u662f Tokio \u5728\u7528\u6237\u6001\u5b9e\u73b0\u7684\u80fd\u529b\uff1b\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\uff1a<\/p>\n<ul>\n<li>\u5355\u8c03\u65f6\u949f<\/li>\n<li>\u7ebf\u7a0b\u8c03\u5ea6<\/li>\n<li>\u963b\u585e\u4e0e\u5524\u9192<\/li>\n<li>I\/O \u7b49\u5f85\u673a\u5236<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u6982\u62ec\u4e3a\uff1a<\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\uff1a\u8d1f\u8d23\u8ba9\u7ebf\u7a0b\u4f11\u7720\u548c\u9192\u6765<br \/>\nTokio\uff1a\u8d1f\u8d23\u7ba1\u7406\u5b9a\u65f6\u5668\uff0c\u5e76\u51b3\u5b9a\u5524\u9192\u54ea\u4e9b\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>Tokio \u4e0d\u4f1a\u4e3a\u6bcf\u4e2a Sleep \u521b\u5efa\u4e00\u4e2a\u7cfb\u7edf\u7ebf\u7a0b\u6216\u7cfb\u7edf\u5b9a\u65f6\u5668\u3002<\/p>\n<h2>3. Future\u3001Waker \u548c\u65f6\u95f4\u8f6e<\/h2>\n<p>\u5b9a\u65f6 Future \u9996\u6b21\u88ab poll \u65f6\uff1a<\/p>\n<ol>\n<li>\u5c06 deadline \u548c\u5f53\u524d\u4efb\u52a1\u7684 Waker \u6ce8\u518c\u5230\u65f6\u95f4\u8f6e\u3002<\/li>\n<li>\u8fd4\u56de Poll::Pending\u3002<\/li>\n<li>\u5b9a\u65f6\u5668\u5230\u671f\u540e\uff0ctimer driver \u8c03\u7528 Waker\u3002<\/li>\n<li>\u4efb\u52a1\u8fdb\u5165 executor \u7684\u8fd0\u884c\u961f\u5217\u3002<\/li>\n<li>Executor \u518d\u6b21 poll Future\uff0c\u6b64\u65f6\u8fd4\u56de Poll::Ready\u3002<\/li>\n<\/ol>\n<p>\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p>poll Sleep\/Interval<br \/>\n\u2193<br \/>\n\u6ce8\u518c\u65f6\u95f4\u8f6e\uff0c\u8fd4\u56de Pending<br \/>\n\u2193<br \/>\n\u65f6\u95f4\u8f6e\u5230\u671f\uff0c\u8c03\u7528 Waker<br \/>\n\u2193<br \/>\n\u4efb\u52a1\u8fdb\u5165\u8fd0\u884c\u961f\u5217<br \/>\n\u2193<br \/>\nExecutor \u518d\u6b21 poll<br \/>\n\u2193<br \/>\n\u8fd4\u56de Ready<\/p>\n<p>\u5b9a\u65f6\u5668\u5230\u671f\u53ea\u4ee3\u8868\u4efb\u52a1\u5177\u5907\u8fd0\u884c\u6761\u4ef6\uff0c\u4e0d\u4ee3\u8868\u4e1a\u52a1\u4ee3\u7801\u4f1a\u5728\u8be5\u65f6\u523b\u7acb\u5373\u6267\u884c\u3002<\/p>\n<h2>4. Tokio \u5982\u4f55\u4f7f\u7528 epoll<\/h2>\n<p>Linux \u4e0b\uff0cTokio \u901a\u5e38\u901a\u8fc7 Mio \u4f7f\u7528 epoll \u7b49\u5f85 I\/O\u3002<\/p>\n<p>epoll_wait \u672c\u8eab\u652f\u6301 timeout\uff1a<\/p>\n<p>epoll_wait(epoll_fd, events, max_events, timeout_ms);<\/p>\n<p>Tokio \u4f1a\u4ece\u65f6\u95f4\u8f6e\u4e2d\u627e\u5230\u6700\u8fd1\u7684 deadline\uff0c\u5e76\u628a\u5269\u4f59\u65f6\u95f4\u4f5c\u4e3a timeout\u3002<\/p>\n<p>\u4f8b\u5982\u6700\u8fd1\u5b9a\u65f6\u5668\u8fd8\u6709 200ms\uff1a<\/p>\n<p>epoll_wait(timeout = 200ms)<\/p>\n<p>\u5b83\u53ef\u80fd\u56e0\u4e3a\u4e24\u79cd\u539f\u56e0\u8fd4\u56de\uff1a<\/p>\n<ul>\n<li>I\/O \u63d0\u524d\u5230\u8fbe\uff1a\u8fd4\u56de\u5bf9\u5e94 I\/O \u4e8b\u4ef6\u3002<\/li>\n<li>200ms \u5185\u6ca1\u6709 I\/O\uff1a\u8d85\u65f6\u8fd4\u56de 0\u3002<\/li>\n<\/ul>\n<p>\u8d85\u65f6\u8fd4\u56de\u540e\uff0cTokio\u63a8\u8fdb\u65f6\u95f4\u8f6e\u5e76\u5524\u9192\u5230\u671f\u4efb\u52a1\u3002<\/p>\n<p>\u91cd\u8981\u7684\u662f\uff1a\u5b9a\u65f6\u5668\u672c\u8eab\u901a\u5e38\u6ca1\u6709\u9010\u4e2a\u6ce8\u518c\u5230 epoll\u3002epoll \u53ea\u8d1f\u8d23\u7b49\u5f85\u5230\u6700\u8fd1 deadline\u3002<\/p>\n<h2>5. I\/O \u63d0\u524d\u5230\u8fbe\u540e\u7684\u5904\u7406<\/h2>\n<p>\u5047\u8bbe\uff1a<\/p>\n<p>\u6700\u8fd1\u5b9a\u65f6\u5668\u8fd8\u6709 200ms<br \/>\n\u2192 epoll_wait(200ms)<br \/>\n\u2192 50ms \u540e I\/O \u5230\u8fbe<\/p>\n<p>Tokio \u88ab\u63d0\u524d\u5524\u9192\u540e\u4f1a\uff1a<\/p>\n<ol>\n<li>\u5904\u7406 I\/O \u4e8b\u4ef6\u3002<\/li>\n<li>\u5524\u9192\u76f8\u5173 Future\u3002<\/li>\n<li>\u68c0\u67e5\u671f\u95f4\u662f\u5426\u5df2\u6709\u5b9a\u65f6\u5668\u5230\u671f\u3002<\/li>\n<li>\u6267\u884c\u8fd0\u884c\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u3002<\/li>\n<li>\u91cd\u65b0\u67e5\u8be2\u65f6\u95f4\u8f6e\u4e2d\u6700\u8fd1\u7684 deadline\u3002<\/li>\n<li>\u8ba1\u7b97\u65b0\u7684 timeout\uff0c\u518d\u6b21\u8c03\u7528 epoll_wait\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\u5904\u7406 I\/O \u8017\u65f6 20ms\uff0c\u90a3\u4e48\u539f\u5b9a\u65f6\u5668\u7ea6\u5269 130ms\uff1a<\/p>\n<p>epoll_wait(130ms)<\/p>\n<p>\u56e0\u4e3a\u65f6\u95f4\u8f6e\u4fdd\u5b58\u7684\u662f\u7edd\u5bf9 deadline\uff0c\u6240\u4ee5\u4e0d\u4f1a\u56e0 I\/O \u63d0\u524d\u5524\u9192\u800c\u4e22\u5931\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<h2>6. worker \u7ebf\u7a0b\u7684 park<\/h2>\n<p>\u5f53 Tokio worker \u6ca1\u6709\u53ef\u6267\u884c\u4efb\u52a1\u65f6\uff0c\u4f1a\u8fdb\u5165 park\uff1a<\/p>\n<p>\u7b49\u5f85 I\/O<br \/>\n\u6216\u7b49\u5f85\u6700\u8fd1\u5b9a\u65f6\u5668<br \/>\n\u6216\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u63d0\u4ea4\u65b0\u4efb\u52a1<\/p>\n<p>park \u65f6\u7ebf\u7a0b\u88ab\u64cd\u4f5c\u7cfb\u7edf\u963b\u585e\uff0c\u4e0d\u5360\u7528 CPU\u3002<\/p>\n<p>\u9700\u8981\u533a\u5206\uff1a<\/p>\n<ul>\n<li>Future Pending\uff1a\u67d0\u4e2a\u5f02\u6b65\u4efb\u52a1\u6682\u505c\u3002<\/li>\n<li>worker park\uff1a\u6574\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u6682\u65f6\u6ca1\u6709\u4efb\u52a1\u53ef\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u6709\u4e8b\u4ef6\u540e\uff0c\u7ebf\u7a0b\u5148\u8fdb\u5165\u7cfb\u7edf\u5c31\u7eea\u72b6\u6001\uff0c\u83b7\u5f97 CPU \u540e\u624d\u80fd\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n<h2>7. \u4e3a\u4ec0\u4e48 tick \u4f1a\u665a\u5230<\/h2>\n<p>\u65f6\u95f4\u8f6e deadline \u5230\u671f\u540e\uff0c\u4efb\u52a1\u4ecd\u9700\u7b49\u5f85 executor \u518d\u6b21 poll\u3002\u4ee5\u4e0b\u56e0\u7d20\u53ef\u80fd\u9020\u6210\u665a\u5230\uff1a<\/p>\n<ul>\n<li>worker \u6b63\u5728\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff1b<\/li>\n<li>\u67d0\u4e2a Future \u957f\u65f6\u95f4\u6ca1\u6709\u8ba9\u51fa\u6267\u884c\u6743\uff1b<\/li>\n<li>\u540c\u4e00 select! \u5faa\u73af\u7684\u5176\u4ed6\u5206\u652f\u6b63\u5728 .await\uff1b<\/li>\n<li>runtime \u6216\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u7e41\u5fd9\uff1b<\/li>\n<li>\u5168\u91cf\u62c9\u53d6\u7b49\u64cd\u4f5c\u963b\u585e\u4e86\u5b9a\u65f6\u5faa\u73af\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff1a<\/p>\n<p>\u8ba1\u5212\u5230\u671f\u65f6\u95f4 \u2260 \u5b9e\u9645 poll \u65f6\u95f4<\/p>\n<h2>8. Interval \u7684 missed-tick \u7b56\u7565<\/h2>\n<p>Tokio Interval \u652f\u6301\u4e09\u79cd\u7b56\u7565\uff1a<\/p>\n<ul>\n<li>Burst\uff1a\u9ed8\u8ba4\uff0c\u5feb\u901f\u8865\u6267\u884c\u9519\u8fc7\u7684 tick\u3002<\/li>\n<li>Delay\uff1a\u4ece\u5b9e\u9645\u6267\u884c\u65f6\u95f4\u91cd\u65b0\u8ba1\u7b97\u4e0b\u4e00\u5468\u671f\u3002<\/li>\n<li>Skip\uff1a\u8df3\u8fc7\u9519\u8fc7\u7684 tick\uff0c\u4fdd\u6301\u539f\u59cb\u5468\u671f\u8282\u594f\u3002<\/li>\n<\/ul>\n<p>\u6ce8\u518c\u5fc3\u8df3\u901a\u5e38\u66f4\u9002\u5408 Skip\uff0c\u907f\u514d\u957f\u65f6\u95f4\u963b\u585e\u540e\u96c6\u4e2d\u8865\u6ce8\u518c\u3002<\/p>\n<p>\u6b64\u524d\u7684 panic \u662f\u56e0\u4e3a\u4f7f\u7528\uff1a<\/p>\n<p>interval_at(start, Duration::MAX)<\/p>\n<p>tick \u665a\u5230\u8d85\u8fc7 5ms \u540e\uff0c\u9ed8\u8ba4 Burst \u8ba1\u7b97\uff1a<\/p>\n<p>next = \u539f\u8ba1\u5212\u65f6\u95f4 + Duration::MAX<\/p>\n<p>\u5bfc\u81f4 Instant \u6ea2\u51fa\u3002\u6839\u672c\u539f\u56e0\u662f\u7528\u8d85\u5927\u5468\u671f\u7684 interval \u6a21\u62df\u4e00\u6b21\u6027\u4efb\u52a1\uff1b\u6b63\u786e\u5b9e\u73b0\u5e94\u4f7f\u7528 sleep\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u65f6\u95f4\u8f6e\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898 \u65f6\u95f4\u8f6e\u7528\u4e8e\u9ad8\u6548\u7ba1\u7406\u5927\u91cf\u5b9a\u65f6\u4efb\u52a1\uff0c\u4f8b\u5982\uff1a \u8bf7\u6c42\u8d85\u65f6 \u5fc3\u8df3 \u5ef6\u8fdf\u91cd\u8bd5 tokio::ti [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,8],"tags":[20,24],"class_list":["post-640","post","type-post","status-publish","format-standard","hentry","category-linux","category-rust","tag-linux","tag-rust"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=640"}],"version-history":[{"count":1,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/640\/revisions"}],"predecessor-version":[{"id":641,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/640\/revisions\/641"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}