制作一个可以从其他Go应用程序调用的守护进程。

huangapple go评论173阅读模式
英文:

Go: Making a daemon that is callable from other Go apps

问题

我正在制作一个巨大的单词-语言字典,我已经有了数据,但我需要有一个在后台运行的线程,用Go语言编写的守护进程,将所有数据保存在内存中(是的,我有那么多内存),并且可以被其他Go应用程序调用。

我相信这是一种标准的做法,但老实说,我以前从未尝试过这样的事情,也不熟悉在哪里找到有关如何做到这一点的信息。

将其作为守护进程运行很容易。我的问题是如何高效地从另一个Go应用程序调用此应用程序,因为需要执行成千上万次。

我考虑的是类似于以下的方式:

connection, err := InitateConnectionToApp()
for _, someword := range mysliceofstrings {
    languageofword := connection.FindIt(someword)
}

然后守护进程以某种方式接收此请求,查找其映射中的值并将其返回。

希望这样说得清楚。我已经尝试在Google上搜索,但没有找到与Go相关的具体信息。

如果有人告诉我从哪里开始,那就太好了。

英文:

I'm working on a giant dictionary of words -> language, the data for which I have, but what I need is to have one thread running a daemon, written in Go, which keeps all of this in memory (yes, I have that much memory too) and is "callable" by other Go apps.

I'm sure this is a standard type of thing to do, but to be honest I've never attempted such a thing before and am not familiar enough to know where to find information on how to do this.

Having it run as a daemon is easy. My problem is what is an efficient way of calling this app from another Go app, which will need to be done many millions of times.

I'm thinking something along the lines of:

connection, err := InitateConnectionToApp()
for _, someword := range mysliceofstrings {
    languageofword := connection.FindIt(someword)
}

Then the daemon somehow receives this request, looks up the value in its map and delivers it back.

I hope that makes sense. I have tried looking on Google but there is nothing I can find specific to Go.

If anyone can tell me where to start that would be great.

答案1

得分: 1

你可以使用Go语言的标准远程过程调用(RPC)包RPC

只需公开你的API,然后创建一个客户端来远程调用方法。

以下是从文档中复制粘贴的简单示例:

package server

type Args struct {
    A, B int
}

type Quotient struct {
    Quo, Rem int
}

type Arith int

func (t *Arith) Multiply(args *Args, reply *int) error {
    *reply = args.A * args.B
    return nil
}

func (t *Arith) Divide(args *Args, quo *Quotient) error {
    if args.B == 0 {
        return errors.New("divide by zero")
    }
    quo.Quo = args.A / args.B
    quo.Rem = 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)
}
英文:

You could use RPC Go's standard Remote Procedure Call package.

Just Expose your api and then create a client to invoke the method remotely.

Simple example copy pasted from the docs:

package server

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

type Arith int

func (t *Arith) Multiply(args *Args, reply *int) error {
	*reply = args.A * args.B
	return nil
}

func (t *Arith) Divide(args *Args, quo *Quotient) error {
	if args.B == 0 {
		return errors.New("divide by zero")
	}
	quo.Quo = args.A / args.B
	quo.Rem = 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)
}

huangapple
  • 本文由 发表于 2014年8月2日 02:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/25086652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定