如果我的状态不改变,是否需要OTP? [英] is OTP needed if my state does not change?

查看:118
本文介绍了如果我的状态不改变,是否需要OTP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个erlang项目,其状态是完全只读的,由ets表和一个编译模块。代码开始时建立表需要几秒钟。



最好的打包方式是什么,以便其他应用程序使用? / strong>



需要考虑的其他一些事情:




  • 没有数据争用问题或改变状态,所以似乎不需要gen_server。

  • 我喜欢可以调用应用程序:start / 1 并且有东西正常工作。

  • 某些进程应该拥有ets表。看来我不应该把它留在客户端代码中。



以上引导我认为我应该创建一个应用程序,从主管的 init / 1 函数调用安装代码,但我不确定这是否是一个愚蠢的方法来处理它。

解决方案

如果模块是完全静态的,那么在这样的应用程序中不需要任何进程。如果您在OTP中实现应用程序行为,则应该能够将ETS表直接连接到主应用程序进程(启动应用程序中的表 start / 2 回调)。这样,您将拥有一个最小的流程模型,允许您跳过主管和任何 gen_server



你一定要使用OTP,但这并不意味着你绝对必须有一个主管或$ code> gen_server 。



使用

I have an erlang project whose state is entirely read only, and composed of ets tables and a compiled module. It takes a few seconds to build the tables when the code starts.

What is the best way to package this so it can be used by other applications?

Some other things to consider:

  • There are no data contention issues or changing state, so it seems as if there is no need for a gen_server.
  • I like being able to call application:start/1 and have things "just work".
  • Some process should own the ets tables. It seems like I should not leave that up to the client code.

The above leads me to think I should create an application, and call the setup code from the supervisor's init/1 function, but I'm unsure if this is a silly way to approach it.

解决方案

If the module is entirely static you won't need any processes in such an application. If you implement the application behavior in OTP you should be able to connect the ETS tables directly to the main application process (initiate the tables in the application start/2 callback). That way you'll have a minimal process model, allowing you to skip both the supervisor and any gen_server.

You should certainly use OTP, but that doesn't mean that you absolutely must have a supervisor or a gen_server.

Use appmon to view the process hierarchy of your application.

这篇关于如果我的状态不改变,是否需要OTP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆