golang fasthttp是单线程的?

查看:405
本文介绍了golang fasthttp是单线程的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

requestHandler := func(ctx *fasthttp.RequestCtx) {
        // 睡眠十秒
        time.Sleep(time.Second*time.Duration(10))
        fmt.Fprintf(ctx, "Hello, world! Requested path is %q", ctx.Path())
    }


    s := &fasthttp.Server{
    Handler: requestHandler,
    }

    if err := s.ListenAndServe("127.0.0.1:82"); err != nil {
    log.Fatalf("error in ListenAndServe: %s", err)
    }

逻辑就是睡眠10秒,开几个页面同时请求,花费的时间基本就是单线程顺序执行。。。。

解决方案

我试了以下,没什么问题,

修改了你的代码

requestHandler := func(ctx *fasthttp.RequestCtx) {
    fmt.Println("收到请求时间:", time.Now().Unix())
    // 睡眠十秒
    time.Sleep(time.Second * time.Duration(10))
    fmt.Fprintf(ctx, "Hello, world! Requested path is %q", ctx.Path())
    fmt.Println("处理完请求时间:", time.Now().Unix())
}

分别四次请求输出结果:

收到请求时间: 1500716082
收到请求时间: 1500716083
收到请求时间: 1500716084
收到请求时间: 1500716085
处理完请求时间: 1500716092
处理完请求时间: 1500716093
处理完请求时间: 1500716094
处理完请求时间: 1500716095

完全是预期的结果,是平行执行的,不存在顺序执行。

这篇关于golang fasthttp是单线程的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆