<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>异步编程 - 标签 - 研发日志 · R&amp;D Log</title><link>https://rd163.visword.com/tags/%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B/</link><description>异步编程 - 标签 - 研发日志 · R&amp;D Log</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><managingEditor>whutluohui@gmail.com (小智晖)</managingEditor><webMaster>whutluohui@gmail.com (小智晖)</webMaster><copyright>本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。</copyright><lastBuildDate>Sun, 07 Sep 2025 00:00:00 +0800</lastBuildDate><atom:link href="https://rd163.visword.com/tags/%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B/" rel="self" type="application/rss+xml"/><item><title>Python asyncio 的事件循环机制</title><link>https://rd163.visword.com/posts/python-asyncio-event-loop/</link><pubDate>Sun, 07 Sep 2025 00:00:00 +0800</pubDate><author><name>小智晖</name></author><guid>https://rd163.visword.com/posts/python-asyncio-event-loop/</guid><description>&lt;p>在使用 Python 的 asyncio 库实现异步编程时，协程（coroutine）与事件循环（event loop）这两个概念总是形影不离：协程只声明了「怎么挂起、怎么恢复」,而真正驱动这些协程不断推进的「调度器」,就是事件循环。理解事件循环的角色，是写出正确的异步代码、以及做性能选型（比如要不要换 uvloop）的前提。&lt;/p></description></item><item><title>boost.asio 网络编程</title><link>https://rd163.visword.com/posts/boost-asio-cpp-network/</link><pubDate>Tue, 03 Jun 2025 00:00:00 +0800</pubDate><author><name>小智晖</name></author><guid>https://rd163.visword.com/posts/boost-asio-cpp-network/</guid><description><![CDATA[<h2 id="引言" class="headerLink">
    <a href="#%e5%bc%95%e8%a8%80" class="header-mark"></a>引言</h2><p>在 C++ 后端开发领域，<strong>Boost.Asio</strong>（Asynchronous Input/Output）几乎是事实上的网络与底层 I/O 标准库。它不仅支撑了大量开源项目（如 websocketpp、cpp-httplib 的部分实现思路），也是 C++ 标准委员会 <strong>Networking TS</strong> 的原型基础。本文整理 Asio 的核心概念、设计模式以及实战要点，帮助你在写第一个高并发服务端时不踩坑。</p>]]></description></item><item><title>Js 协程简介</title><link>https://rd163.visword.com/posts/js-corouting-intro/</link><pubDate>Sun, 13 Apr 2025 00:00:00 +0800</pubDate><author><name>小智晖</name></author><guid>https://rd163.visword.com/posts/js-corouting-intro/</guid><description><![CDATA[<p>这篇文章 <a href="https://zh.javascript.info/async" target="_blank" rel="noopener noreferrer">JavaScript Promise，async/await</a> 是对 async/await 最详细的介绍，而且 文章开源：</p>
<ul>
<li><a href="https://github.com/javascript-tutorial/zh.javascript.info" target="_blank" rel="noopener noreferrer">现代 JavaScript 教程中文版</a></li>
</ul>
<h2 id="callback回调" class="headerLink">
    <a href="#callback%e5%9b%9e%e8%b0%83" class="header-mark"></a>callback(回调)</h2><p>回调地狱：</p>
<div class="code-block highlight is-closed show-line-numbers  tw:group tw:my-2">
  <div class="
    
    tw:flex 
    tw:flex-row
    tw:flex-1 
    tw:justify-between 
    tw:w-full tw:bg-bgColor-secondary
    ">      
    <button 
      class="
        code-block-button
        tw:mx-2 
        tw:flex
        tw:flex-row
        tw:flex-1"
      aria-hidden="true">
          <span class="tw:group-[.is-open]:rotate-90 tw:transition-[transform] tw:duration-500 tw:ease-in-out tw:print:hidden! tw:w-min tw:h-min tw:my-1 tw:mx-1"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"/></svg></span>
          <span class="tw:select-none tw:my-1! tw:block">js</span>
      </button>

   <div class="tw:flex">
      <button 
        class="
          line-number-button
          tw:mx-2 
          tw:hidden 
          tw:group-[.is-open]:block 
          tw:group-[.show-line-numbers]:text-fgColor-link 
          tw:print:hidden!" 
        title="Toggle line numbers"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M61.77 401l17.5-20.15a19.92 19.92 0 0 0 5.07-14.19v-3.31C84.34 356 80.5 352 73 352H16a8 8 0 0 0-8 8v16a8 8 0 0 0 8 8h22.83a157.41 157.41 0 0 0-11 12.31l-5.61 7c-4 5.07-5.25 10.13-2.8 14.88l1.05 1.93c3 5.76 6.29 7.88 12.25 7.88h4.73c10.33 0 15.94 2.44 15.94 9.09 0 4.72-4.2 8.22-14.36 8.22a41.54 41.54 0 0 1-15.47-3.12c-6.49-3.88-11.74-3.5-15.6 3.12l-5.59 9.31c-3.72 6.13-3.19 11.72 2.63 15.94 7.71 4.69 20.38 9.44 37 9.44 34.16 0 48.5-22.75 48.5-44.12-.03-14.38-9.12-29.76-28.73-34.88zM496 224H176a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h320a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zm0-160H176a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h320a16 16 0 0 0 16-16V80a16 16 0 0 0-16-16zm0 320H176a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h320a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zM16 160h64a8 8 0 0 0 8-8v-16a8 8 0 0 0-8-8H64V40a8 8 0 0 0-8-8H32a8 8 0 0 0-7.14 4.42l-8 16A8 8 0 0 0 24 64h8v64H16a8 8 0 0 0-8 8v16a8 8 0 0 0 8 8zm-3.91 160H80a8 8 0 0 0 8-8v-16a8 8 0 0 0-8-8H41.32c3.29-10.29 48.34-18.68 48.34-56.44 0-29.06-25-39.56-44.47-39.56-21.36 0-33.8 10-40.46 18.75-4.37 5.59-3 10.84 2.8 15.37l8.58 6.88c5.61 4.56 11 2.47 16.12-2.44a13.44 13.44 0 0 1 9.46-3.84c3.33 0 9.28 1.56 9.28 8.75C51 248.19 0 257.31 0 304.59v4C0 316 5.08 320 12.09 320z"/></svg></button>

      <button 
        class="
          wrap-code-button
          tw:select-none 
          tw:mx-2 
          tw:hidden 
          tw:group-[.is-open]:block 
          tw:group-[.is-wrap]:text-fgColor-link 
          tw:print:hidden!" 
        title="Toggle code wrap"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M16 132h416c8.837 0 16-7.163 16-16V76c0-8.837-7.163-16-16-16H16C7.163 60 0 67.163 0 76v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16z"/></svg></button>
      
      <button 
        class="
          copy-code-button
          tw:select-none
          tw:mx-2 
          tw:hidden
          tw:group-[.is-open]:block
          tw:hover:text-fgColor-link 
          tw:print:hidden!"
        title="Copy code">
          <span class="copy-icon tw:block"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M433.941 65.941l-51.882-51.882A48 48 0 0 0 348.118 0H176c-26.51 0-48 21.49-48 48v48H48c-26.51 0-48 21.49-48 48v320c0 26.51 21.49 48 48 48h224c26.51 0 48-21.49 48-48v-48h80c26.51 0 48-21.49 48-48V99.882a48 48 0 0 0-14.059-33.941zM266 464H54a6 6 0 0 1-6-6V150a6 6 0 0 1 6-6h74v224c0 26.51 21.49 48 48 48h96v42a6 6 0 0 1-6 6zm128-96H182a6 6 0 0 1-6-6V54a6 6 0 0 1 6-6h106v88c0 13.255 10.745 24 24 24h88v202a6 6 0 0 1-6 6zm6-256h-64V48h9.632c1.591 0 3.117.632 4.243 1.757l48.368 48.368a6 6 0 0 1 1.757 4.243V112z"/></svg></span>
          <span class="check-icon tw:hidden"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M173.898 439.404l-166.4-166.4c-9.997-9.997-9.997-26.206 0-36.204l36.203-36.204c9.997-9.998 26.207-9.998 36.204 0L192 312.69 432.095 72.596c9.997-9.997 26.207-9.997 36.204 0l36.203 36.204c9.997 9.997 9.997 26.206 0 36.204l-294.4 294.401c-9.998 9.997-26.207 9.997-36.204-.001z"/></svg></span>
      </button>
        
      <button 
        class="
          tw:select-none 
          tw:mx-2 
          tw:block 
          tw:group-[.is-open]:hidden 
          tw:print:hidden!" 
        disabled
        aria-hidden="true"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M328 256c0 39.8-32.2 72-72 72s-72-32.2-72-72 32.2-72 72-72 72 32.2 72 72zm104-72c-39.8 0-72 32.2-72 72s32.2 72 72 72 72-32.2 72-72-32.2-72-72-72zm-352 0c-39.8 0-72 32.2-72 72s32.2 72 72 72 72-32.2 72-72-32.2-72-72-72z"/></svg></button>
    </div>
  </div>
  <pre style="counter-reset: codeblock;" class="tw:block tw:m-0 tw:p-0"><code 
    id="codeblock-id-103" 
    class="
      chroma 
      tw:block! 
      tw:p-0
      tw:m-0
      tw:transition-[max-height] 
      tw:duration-500 
      tw:ease-in-out 
      tw:group-[.is-closed]:max-h-0! 
      tw:group-[.is-wrap]:text-wrap
      tw:overflow-y-hidden
      tw:overflow-x-auto
      tw:scrollbar-thin
      "><span class="line"><span class="cl"><span class="nx">loadScript</span><span class="p">(</span><span class="s1">&#39;1.js&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">script</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">if</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">handleError</span><span class="p">(</span><span class="nx">error</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// ...
</span></span></span><span class="line"><span class="cl">    <span class="nx">loadScript</span><span class="p">(</span><span class="s1">&#39;2.js&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">script</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="k">if</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="nx">handleError</span><span class="p">(</span><span class="nx">error</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// ...
</span></span></span><span class="line"><span class="cl">        <span class="nx">loadScript</span><span class="p">(</span><span class="s1">&#39;3.js&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">script</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">          <span class="k">if</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="nx">handleError</span><span class="p">(</span><span class="nx">error</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">          <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="c1">// ...加载完所有脚本后继续 (*)
</span></span></span><span class="line"><span class="cl">          <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="p">});</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">      <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">});</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">});</span></span></span></code></pre>
</div>
<p>嵌套调用的&quot;金字塔&quot;随着每个异步行为会向右增长。很快它就失控了。</p>]]></description></item><item><title>Python 协程简介</title><link>https://rd163.visword.com/posts/python-corouting-intro/</link><pubDate>Sun, 13 Apr 2025 00:00:00 +0800</pubDate><author><name>小智晖</name></author><guid>https://rd163.visword.com/posts/python-corouting-intro/</guid><description><![CDATA[<p>Python 在 3.5 版本中引入了协程相关的语法糖 <code>async</code> 和 <code>await</code>，在 Python 3.7 版本中又提供了 <code>asyncio.run()</code> 来运行一个协程。因此建议大家学习协程时直接使用 Python 3.7 及以上版本。</p>
<p>Python 官方提供了各版本的 asyncio 文档，详见<a href="https://docs.python.org/zh-cn/3.13/library/asyncio-task.html" target="_blank" rel="noopener noreferrer">协程与任务官方文档</a>。</p>]]></description></item><item><title>Boost.Asio 简介</title><link>https://rd163.visword.com/posts/cpp-boost-asio-intro/</link><pubDate>Tue, 08 Apr 2025 00:00:00 +0800</pubDate><author><name>小智晖</name></author><guid>https://rd163.visword.com/posts/cpp-boost-asio-intro/</guid><description><![CDATA[<h2 id="概述" class="headerLink">
    <a href="#%e6%a6%82%e8%bf%b0" class="header-mark"></a>概述</h2><p>Boost.Asio 是一个跨平台的、主要用于网络和其他一些底层输入/输出编程的 C++ 库。</p>
<p>Asio，即「异步 IO」(Asynchronous Input/Output),本是一个<a href="http://think-async.com/Asio" target="_blank" rel="noopener noreferrer">独立的 C++ 网络程序库</a>,似乎并不为人所知，后来因为被 Boost 相中，才声名鹊起。</p>]]></description></item><item><title>C++ Promise/Future 模型</title><link>https://rd163.visword.com/posts/cpp-promise-future-intro/</link><pubDate>Sun, 06 Apr 2025 00:00:00 +0800</pubDate><author><name>小智晖</name></author><guid>https://rd163.visword.com/posts/cpp-promise-future-intro/</guid><description>&lt;p>Promise/Future 是一种经典的异步抽象，正因为它提供了一套「标准化」的概念，因而衍生出许多可能性。这就如同工业史上的集装箱、软件界的 Docker——凭借「标准化」这一关键属性，不同组件可以简单地自由组合，从而大幅提升整体效率。&lt;/p></description></item><item><title>c++20协程简介</title><link>https://rd163.visword.com/posts/cpp20-corouting-intro/</link><pubDate>Sun, 06 Apr 2025 00:00:00 +0800</pubDate><author><name>小智晖</name></author><guid>https://rd163.visword.com/posts/cpp20-corouting-intro/</guid><description><![CDATA[<h2 id="c20-协程简介" class="headerLink">
    <a href="#c20-%e5%8d%8f%e7%a8%8b%e7%ae%80%e4%bb%8b" class="header-mark"></a>C++20 协程简介</h2><p>参考：</p>
<ul>
<li><a href="https://github.com/0voice/cpp_backend_awsome_blog/blob/main/%E3%80%90NO.241%E3%80%91%E5%8D%8F%E7%A8%8B%E5%8F%8Ac%2B%2B%2020%E5%8E%9F%E7%94%9F%E5%8D%8F%E7%A8%8B%E7%A0%94%E7%A9%B6%E6%8A%A5%E5%91%8A.md" target="_blank" rel="noopener noreferrer">协程及 C++ 20 原生协程研究报告</a></li>
<li><a href="https://0cch.com/uploads/2022/02/k&#43;2021.pdf" target="_blank" rel="noopener noreferrer">2021K+ 全球软件研发行业创新峰会：深入解析 C++20 协程（PDF）</a></li>
</ul>
<p>C++20 协程通过 Promise 和 Awaitable 接口的十余个函数，向程序员暴露出定制协程流程和行为的入口。实现一个最简单的协程，通常需要用到其中的 8 个（5 个 Promise 函数 + 3 个 Awaitable 函数）。下面先从 Awaitable 的 3 个函数说起。</p>]]></description></item></channel></rss>