【问题标题】:Go: Making a daemon that is callable from other Go appsGo:创建一个可以从其他 Go 应用程序调用的守护进程
【发布时间】:2014-08-01 18:41:27
【问题描述】:

我正在研究一个巨大的单词词典 -> 语言,我拥有的数据,但我需要的是让一个线程运行一个用 Go 编写的守护程序,它将所有这些都保存在内存中(是的,我也有这么多内存)并且可以被其他 Go 应用程序“调用”。

我确信这是一种标准类型的事情,但老实说,我以前从未尝试过这样的事情,而且我还不够熟悉,不知道在哪里可以找到有关如何执行此操作的信息。

让它作为守护进程运行很容易。我的问题是从另一个 Go 应用程序调用这个应用程序的有效方法是什么,这需要完成数百万次。

我的想法是这样的:

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

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

我希望这是有道理的。我曾尝试在 Google 上查找,但找不到特定于 Go 的内容。

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

【问题讨论】:

  • 也许只是从公开一个 http api 开始?
  • 我认为你正在寻找类似 reddis 的东西
  • 不,我想做类似 reddis 的东西。

标签: go daemon


【解决方案1】:

你可以使用RPC Go 的标准远程过程调用包。

只需公开您的 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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多