Fanx 语言 3.3 发布,使用 async-await 解决异步 IO 问题

栏目: 软件资讯 · 发布时间: 5年前

内容简介:Fanx是面向对象的函数式的编程语言。 3.3版本主要增强async-await功能,并尝试用其来解决异步IO问题。 之前async-await用在客户端UI框架的网络请求模块,这次将其用在服务端。传统的阻塞式IO性能较低,而异步IO有...

Fanx是面向对象的函数式的编程语言。 3.3版本主要增强async-await功能,并尝试用其来解决异步IO问题。

之前async-await用在客户端UI框架的网络请求模块,这次将其用在服务端。传统的阻塞式IO性能较低,而异步IO有很多回调,导致代码很难阅读。使用async-await可以使我们用类似阻塞IO的方式编写代码,同时拥有异步IO的性能。

目前的网络框架支持基本的http协议,示例代码:

const class HttpTestServer : HttpHandler {

  override async Void onHttpService(HttpReq req, HttpRes res) {
    res.headers["Content-Type"] = "text/html; charset=utf-8"

    buf := NioBuf.makeMem(1024)
    buf.printLine("<html>
                        <body>Hello World $req.uri</body>
                       </html>")
    buf.flip
    await res.writeChunk(buf)
  }

  static Void main() {
    Server {
      port = 8080
      handler = HttpTestServer()
    }.start
  }
}

代码中,当运行到await的时候当前任务暂停,并交给selector线程去监听IO事件,当前线程没有阻塞而是去继续处理其他连接任务。当selector发现事件就绪后通知恢复运行任务。

无论是web开发还是App开发,Fanx语言可以在前后端开发中使用同一种语言,避免了中间的隔阂。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Iterative Methods for Sparse Linear Systems, Second Edition

Iterative Methods for Sparse Linear Systems, Second Edition

Yousef Saad / Society for Industrial and Applied Mathematics / 2003-04-30 / USD 102.00

Tremendous progress has been made in the scientific and engineering disciplines regarding the use of iterative methods for linear systems. The size and complexity of linear and nonlinear systems arisi......一起来看看 《Iterative Methods for Sparse Linear Systems, Second Edition》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具