9

I understand how a process works. And I understand how to implement OTP behaviours such as gen-server, etc.

But It's not clear to me when I would chose one approach over the other.

For instance, Cesarini et. al., in Erlang Programming, implement the database server on p 240 by spawning a process, writing a loop function, etc. Joe Armstrong's chat client on p 196 in Programming Erlang also spawns a process.

Why wouldn't these be better implemented as OTP gen-servers? Is it for educational purposes? Or are there sound technical reasons?

In other words, what rules of thumb would guide me to implement one approach over the other?

Many thanks.

4

1 回答 1

7

在上面的两个示例中,出于教育目的,首选简单的衍生过程:这更容易解释,这些作者不必解释 OTP 来传达他们想要教的内容,无论如何理解过程的概念很重要二郎。即使在遵循 OTP 规则的实际应用程序中,并非所有流程都使用 OTP 行为实现并插入到监督树中。

经验法则非常简单。通过查看 OTP 设计原则(代码更改、容错等)来确定您是否要遵循 OTP 设计原则,或者您是否计划与 OTP 兼容代码集成。如果您想要它带来的任何功能并避免重新发明轮子,请遵循 OTP 原则。

如果您坚持 OTP 原则并因此使用行为,您应该为任何不需要监督或代码升级的短期流程gen_*做一个简单的衍生。它的寿命不应超过部署发布的时间(否则,无论如何你都需要)。所有其他进程必须插入​​到监督树中,并且可能实现 gen_* 行为。soft_purge

于 2013-10-02T20:26:53.713 回答