何时在 Erlang/OTP 应用程序中使用 gen_server

发布于 2024-08-03 16:20:55 字数 425 浏览 3 评论 0原文

阅读 Joe Armstrong 的书并观看 Kevin Smith 的截屏视频后,我构建了一个简单的 OTP 应用程序,由单个 gen_server 和单个 Supervisor 组成,捆绑在一起作为 OTP 应用程序。

现在我正在研究 mochiweb 并使用 new_mochiweb.erl 脚本创建了一个示例项目 [helloworld]。浏览源代码,我发现它与我的示例 OTP 应用程序没有什么不同 [OTP 应用程序在那里,主管也在那里],但有一个关键区别.. 生成的 helloworld.erl 和 helloworld_web.erl 文件不实现 gen_server 行为,它们只是标准的 Erlang 模块。

我的印象是,在构建 OTP 应用程序组件时,推荐使用 gen_server。为什么 mochiweb 使用 OTP 应用程序和管理程序行为但避开 gen_server ?

Having read Joe Armstrong's book and watched Kevin Smith screencasts I've built a simple OTP application comprised of a single gen_server and single supervisor, bundled together as an OTP application.

Now I'm looking at mochiweb and have created a sample project [helloworld] using the new_mochiweb.erl script. Browsing the source code I see it's not dissimilar from my sample OTP app [the OTP application is there, the supervisor is there] with one key difference .. the generated helloworld.erl and helloworld_web.erl files don't implement gen_server behaviour, they are just standard Erlang modules.

I was under the impression that using gen_server was the recommended way to go when building OTP application components. Why might mochiweb use OTP application and supervisor behaviours but eschew gen_server ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-08-10 16:20:55

您可以将 OTP/gen_servers 用于处于重新启动策略下的进程 - 即它们的重新启动由您控制

对于表示与 Web 浏览器的连接的进程来说,情况并非如此。如果该进程终止,服务器将无法重新启动它 - 因此在 OTP 下运行它是没有意义的。

Mochiweb(和 Yaws)都使用 gen_servers 绑定到侦听端口,然后生成一个无监督进程来处理新连接。

You use OTP/gen_servers for processes which are under a restart strategy - that is the restart of them is in your control.

That is not the case with processes representing connections to web browsers. If that process dies there is no way for the server to restart it - therefore running it under OTP is pointless.

Mochiweb (and Yaws) both use gen_servers to bind to the listening port and then spawn an unsupervised process to handle a new connection.

兔姬 2024-08-10 16:20:55

有一个 gen_server,名为 mochiweb_socket_server。生成的模块只是 gen_server 在收到传入请求时调用的“回调模块”。

There is a gen_server, called mochiweb_socket_server. The generated modules are only "callback modules" for the gen_server to be called when recieveing an incoming request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文