GobEncoder用于通过RPC传递匿名函数

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

GobEncoder for Passing Anonymous Function via RPC

问题

我正在尝试构建一个系统,该系统将通过RPC将函数匿名地传递给每个工作机器(类似于MapReduce),以便在一些数据子集上执行。虽然Gob不支持编码函数,但是GobEncoder的文档中说:“实现GobEncoder和GobDecoder的类型完全控制其数据的表示,并且因此可能包含私有字段、通道和函数等通常不可传输的gob流”。所以这似乎是可能的。

有没有关于如何使用Gob进行编码/解码的示例?我对如何进行编码/解码不太了解。

英文:

I'm trying to build a system that will execute a function on multiple machines, passing the function anonymously via RPC to each worker machine (a la MapReduce) to execute on some subset of data. Gob doesn't support encoding functions, though the docs for GobEncoder say that "A type that implements GobEncoder and GobDecoder has complete control over the representation of its data and may therefore contain things such as private fields, channels, and functions, which are not usually transmissible in gob streams" so it seems possible.

Any examples of how this might work? I don't know much about how this encoding/decoding should be done with Gob.

答案1

得分: 4

在我看来,这是行不通的。虽然如果你的类型实现了Gob{En,De}coder,它可以(反)序列化结构体的未导出字段,但是无法(反)序列化代码:Go是静态编译的,没有运行时代码生成能力(这将绕过编译时类型安全性)。

简而言之:你无法序列化函数,只能序列化数据。你的工作程序必须提供你想要执行的函数。可以看一下encoding/rpc。

英文:

IMHO this won't work. While it is true that if your type implements
Gob{En,De}coder it can (de)serialize unexported fields of structs it is still impossible to (de)serialize code: Go is statically compiled and linked without
runtime code generation capabilities (which would circumvent compile time type
safety).

Short: You cannot serialize functions, only data. Your workers must provide
the functions you wan't to execute. Take a look at encoding/rpc.

答案2

得分: 1

你可以尝试使用GoCircuit,它提供了一个基本框架,可以让你做到这一点:

http://www.gocircuit.org/

它的工作原理是将你的二进制文件复制到远程机器上,启动它,然后进行一个RPC调用,有效地执行“使用参数A、B等执行函数X”。

英文:

You may want to try GoCircuit, which provides a framework that basically lets you do this:

http://www.gocircuit.org/

It works by copying your binary to the remote machine(s), starting it, then doing an RPC that effectively says "execute function X with args A, B, ..."

huangapple
  • 本文由 发表于 2013年4月29日 12:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/16271026.html
匿名

发表评论

匿名网友

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

确定