小编use*_*204的帖子

可以golang启动多线程来处理网络IO

我想开发一个软件来处理来自使用GoLang的多个tcp连接的请求,并在具有10Gb-nic的服务器上运行.

似乎性能不足以在单核上恢复/发送数据.所以我想实现该软件来在多个CPU内核上重新发送/发送数据.

然后我做了一个简单的测试服务器来检查GoLang是否可以在多个cpu核心上重新发送/发送数据.它启动多个(16)goroutines以在同一个侦听器上启动http服务器,并使用ab(Apache Benchmark)作为客户端.

服务器启动后,我看到只有一个线程调用EpollWait,但是服务器启动了18个线程,当我开始使用16个并发测试时,服务器只占用一个核心.

所以问题是:有没有办法在GoLang中启动多个线程来处理来自多个tcp连接的数据recv/send.或者我是否应该调用syscall.EpollWait来制作网络框架,自己动手做?

服务器的测试代码:

package main

import (
  "io"
  "log"
  "net"
  "net/http"
  "runtime"
)

type HandlerFunction struct{}

func (self HandlerFunction) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  data := "Hello"
  //fmt.Printf("data_len=%d\n", len(data))
  io.WriteString(w, string(data))
}

func RoutineFunction(hs *http.Server, l net.Listener) {
  runtime.LockOSThread()
  err := hs.Serve(l)
  if err != nil {
    log.Fatalf("serve fail, err=[%s]", err)
  }
}

func main() {
  runtime.GOMAXPROCS(16)

  l, err := net.Listen("tcp", "0.0.0.0:12345")
  if err != nil {
    log.Fatalf("listen fail, err=[%s]", err)
  }

  for i := 0; …
Run Code Online (Sandbox Code Playgroud)

multithreading epoll go

2
推荐指数
1
解决办法
3347
查看次数

标签 统计

epoll ×1

go ×1

multithreading ×1