rpc Call method 运行 和服务端在同一个go routine中吗?
Does rpc Call method run in the same go routine as the server?
我的服务器:
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func main() {
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error:", e)
}
go http.Serve(l, nil)
}
客户:
client.Call("Arith.Multiply", args, &reply)
我想 Multiply
运行 在另一个 go 程序中? client.Call()
实际上调用了 go server.SomeFun()
?
每个 http 请求都有自己的 goroutine,这是 http.Server
本身的 属性,net/rpc
构建在它之上。
来自 http.Serve
的文档:
Serve accepts incoming HTTP connections on the listener l, creating
a new service goroutine for each. The service goroutines read
requests and then call handler to reply to them. Handler is typically
nil, in which case the DefaultServeMux is used.
并且来自 rpc.HandleHTTP
:
HandleHTTP registers an HTTP handler for RPC messages to DefaultServer on DefaultRPCPath and a debugging handler on
DefaultDebugPath. It is still necessary to invoke http.Serve(),
typically in a go statement.
我的服务器:
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func main() {
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error:", e)
}
go http.Serve(l, nil)
}
客户:
client.Call("Arith.Multiply", args, &reply)
我想 Multiply
运行 在另一个 go 程序中? client.Call()
实际上调用了 go server.SomeFun()
?
每个 http 请求都有自己的 goroutine,这是 http.Server
本身的 属性,net/rpc
构建在它之上。
来自 http.Serve
的文档:
Serve accepts incoming HTTP connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them. Handler is typically nil, in which case the DefaultServeMux is used.
并且来自 rpc.HandleHTTP
:
HandleHTTP registers an HTTP handler for RPC messages to DefaultServer on DefaultRPCPath and a debugging handler on DefaultDebugPath. It is still necessary to invoke http.Serve(), typically in a go statement.