{"componentChunkName":"component---src-templates-best-practice-detail-tsx","path":"/best-practice/2021-02-08-asynchronous-execution","result":{"data":{"currentBlog":{"id":"3717c513-52f7-54cd-9f34-e7f2a52dd587","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/238c88435e45de91ad4475f6e085c2a6.jpg","authors":["李啸川"],"categories":["best-practice"],"date":"2021-02-08T00:00:00.000Z","title":"解锁长时重计算：云函数首创异步执行模式","description":"可用于 2K4K 音视频处理、ETL 数据批处理、机器学习及 AI 推理等单任务重计算等场景","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","异步执行"],"keywords":null,"outdated":null},"wordCount":{"words":117,"sentences":35,"paragraphs":35},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-02-08-asynchronous-execution.md","fields":{"slug":"/best-practice/2021-02-08-asynchronous-execution/","keywords":["serverless","云函数","云原生","函数","执行","异步","同步","serverless","模式","调用"]},"html":"<p>云函数作为新一代通用计算平台的产品化载体，在云原生事件驱动框架下，对轻量的原子计算有较好的支持，但在 2k4K 音视频处理、ETL 数据批处理、机器学习及 AI 推理等单任务重计算的场景下，对云函数的运行机制及现有的上限阈值提出了更多挑战。</p>\n<ul>\n<li>更长时间稳定运行</li>\n<li>单实例更多算力</li>\n<li>对运行中函数更强的状态管控</li>\n<li>执行情况实时反馈</li>\n</ul>\n<p>云函数 SCF 首创提供了一种全新的函数运行机制，函数异步执行模式。在解决以上痛点的同时，可以拓展适用于更多的应用场景。</p>\n<h2 id=\"同步执行模式\"><a href=\"#%E5%90%8C%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F\" aria-label=\"同步执行模式 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>同步执行模式</h2>\n<p>首先对比了解下云函数现有的同步执行模式，以通过 API 网关触发器同步调用云函数为例：</p>\n<p><img src=\"https://main.qcloudimg.com/raw/8f2fed12b32f4ba1389a79e873c5f177.png\"></p>\n<h3 id=\"优点\"><a href=\"#%E4%BC%98%E7%82%B9\" aria-label=\"优点 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>优点</h3>\n<ul>\n<li>全链路串行同步执行，模型简单直观易于理解</li>\n<li>状态精简，逻辑分支、异常处理等成本低</li>\n</ul>\n<p>同步执行模式，非常适合web应用、轻量API、IOT等时间短、计算量小的处理场景。</p>\n<h3 id=\"局限性\"><a href=\"#%E5%B1%80%E9%99%90%E6%80%A7\" aria-label=\"局限性 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>局限性</h3>\n<p>目前同步执行的超时时间上限为900s，整个调用链路涉及多个组件，所有组件都需要保持同步连接状态，并保障自身在连接状态下的稳定性，任意组件出现网络抖动或异常，都会影响调用的成功率。</p>\n<p>另外，客户端需要阻塞等待请求的响应，持续占用客户端资源。在同步执行模式的架构下很难继续拓展为重计算提供更长时间稳定的执行。</p>\n<h2 id=\"异步执行模式\"><a href=\"#%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F\" aria-label=\"异步执行模式 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>异步执行模式</h2>\n<p>同样的API网关触发器同步调用，来看下异步执行模式</p>\n<p><img src=\"https://main.qcloudimg.com/raw/a3c6259f2fc06c2143a0e26dcf91f2ad.png\"></p>\n<h3 id=\"函数异步执行模式特点\"><a href=\"#%E5%87%BD%E6%95%B0%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F%E7%89%B9%E7%82%B9\" aria-label=\"函数异步执行模式特点 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>函数异步执行模式特点</h3>\n<ul>\n<li>异步执行，发起事件调用立即返回事件的调用标识 RequestId，函数运行时并行启动执行</li>\n<li>实时日志，执行日志实时上报，运行情况实时反馈</li>\n<li>状态管理，提供事件状态的统计、查询及终止等事件管理相关服务</li>\n</ul>\n<p>不难看出，运行机制的重新设计，从根本上解耦了对全链路所有组件的稳定性依赖，将稳定运行时间延长至24小时，并提供近乎无上限的扩展性。在此基础上，对长时运行过程中的日志由一次性上传升级为实时上报。</p>\n<p>作为提供基础算力的资源层，以上可以满足支撑数据批处理、大规模分布式计算等任务调度系统的构建，考虑到 Serverless 化产品理念是提供开箱即用、简单高效的研发模型，云函数提供了对事件状态的持久化及相关的管理服务能力，进一步降低了开发者自建和运维任务管理系统的成本。</p>\n<p><img src=\"https://main.qcloudimg.com/raw/d6430bd530dca81cbed4bae9db193c2a.png\"></p>\n<h2 id=\"如何在控制台设置异步执行模式\"><a href=\"#%E5%A6%82%E4%BD%95%E5%9C%A8%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%AE%BE%E7%BD%AE%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F\" aria-label=\"如何在控制台设置异步执行模式 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>如何在控制台设置异步执行模式</h2>\n<ol>\n<li>登录 <a href=\"https://console.cloud.tencent.com/scf/list?rid=16&#x26;ns=default\">云函数控制台</a>，单击左侧导航栏的【函数服务】。</li>\n<li>在主界面上方选择期望创建函数的地域，并单击【新建】，进入函数创建流程。</li>\n<li>选择使用【空白函数】或选择使用【函数模板】来新建函数。</li>\n<li>在“函数配置”页面，展开【高级设置】，并勾选【异步执行】。</li>\n</ol>\n<p><img src=\"https://main.qcloudimg.com/raw/d4c1c700e23db78436ea1cb145a888eb.jpg\"></p>\n<ol start=\"5\">\n<li>单击【完成】即可创建函数。</li>\n</ol>\n<hr>\n<hr>\n<div id='scf-deploy-iframe-or-md'></div>\n<hr>\n<blockquote>\n<p><strong>传送门：</strong></p>\n<ul>\n<li>GitHub: <a href=\"https://github.com/serverless/serverless/blob/master/README_CN.md\">github.com/serverless</a></li>\n<li>官网：<a href=\"https://serverless.com/\">serverless.com</a></li>\n</ul>\n</blockquote>\n<p>欢迎访问：<a href=\"https://serverlesscloud.cn/\">Serverless 中文网</a>，您可以在 <a href=\"https://serverlesscloud.cn/best-practice\">最佳实践</a> 里体验更多关于 Serverless 应用的开发！</p>","tableOfContents":"<ul>\n<li>\n<p><a href=\"/best-practice/2021-02-08-asynchronous-execution/#%E5%90%8C%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F\">同步执行模式</a></p>\n<ul>\n<li><a href=\"/best-practice/2021-02-08-asynchronous-execution/#%E4%BC%98%E7%82%B9\">优点</a></li>\n<li><a href=\"/best-practice/2021-02-08-asynchronous-execution/#%E5%B1%80%E9%99%90%E6%80%A7\">局限性</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"/best-practice/2021-02-08-asynchronous-execution/#%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F\">异步执行模式</a></p>\n<ul>\n<li><a href=\"/best-practice/2021-02-08-asynchronous-execution/#%E5%87%BD%E6%95%B0%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F%E7%89%B9%E7%82%B9\">函数异步执行模式特点</a></li>\n</ul>\n</li>\n<li><a href=\"/best-practice/2021-02-08-asynchronous-execution/#%E5%A6%82%E4%BD%95%E5%9C%A8%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%AE%BE%E7%BD%AE%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%A8%A1%E5%BC%8F\">如何在控制台设置异步执行模式</a></li>\n</ul>"},"previousBlog":{"id":"7687b641-edc1-5b3d-9825-bd3729e47978","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/d32d37dcb538d5804ede488d392bac3d.jpg","authors":["陈涛"],"categories":["best-practice"],"date":"2021-03-01T00:00:00.000Z","title":"如何快速迁移传统 LB 公网业务到 Serverless?","description":"使用 Serverless 云函数负载均衡（Cloud Load Balancer，CLB）触发方式的优势、典型应用场景和使用指引","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","CLB"],"keywords":null,"outdated":null},"wordCount":{"words":158,"sentences":36,"paragraphs":36},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-03-01-clb-serverless.md","fields":{"slug":"/best-practice/2021-03-01-clb-serverless/","keywords":["serverless","云函数","Serverless","函数","场景","服务"]}},"nextBlog":{"id":"78625ce9-87ce-5ba9-aa95-1fd16d80ffd6","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/db9232e1ca6603964ba35e806f87ee80.jpg","authors":["陈涛"],"categories":["best-practice"],"date":"2021-02-04T00:00:00.000Z","title":"腾讯云 Serverless 重试策略配置能力解读","description":"平台的默认重试策略可满足大多数开发者错误重试的诉求，特殊场景下用户可选择自行配置","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","重试策略"],"keywords":null,"outdated":null},"wordCount":{"words":199,"sentences":40,"paragraphs":40},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-02-04-scf-asynchronous.md","fields":{"slug":"/best-practice/2021-02-04-scf-asynchronous/","keywords":["serverless","云函数","重试","调用","错误","异步","函数","配置","并发","超限"]}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"blogId":"3717c513-52f7-54cd-9f34-e7f2a52dd587","previousBlogId":"7687b641-edc1-5b3d-9825-bd3729e47978","nextBlogId":"78625ce9-87ce-5ba9-aa95-1fd16d80ffd6"}}}