erlang 中的事件管理器进程。命名进程或 Pids?
我有事件管理器进程,可以将事件分派给订阅者(例如http_session_created、http_session_destroyed)。如果使用 Pid 而不是命名进程,我必须将其放入函数中才能与事件管理器一起操作,但如果使用命名进程,代码会更清晰。
哪种变体是正确的?
谢谢你!
I have event manager process that dispatches events to subscribers (e.g. http_session_created, http_sesssion_destroyed). If Pid is used instead of named process, I must put it into functions to operate with event manager but if Named process is used, code will be more clear.
Which variant is right?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然命名进程和注册进程没有实际区别,但它会使其成为全局进程。本质上,您是在告诉系统,这是任何人都可以使用的全球服务。
从你的描述来看,听起来更像是你给它们起了名字,以节省在你的循环中携带它们的小小的努力。如果是这种情况,我会将它们的 pid 与您携带的所有其他状态数据一起放入记录中。这更好地表明了进程的类型。
While there is no actual difference to the process naming a process, registering it, makes it global. You in essence you are telling the system that here is a global service which anyone can use.
From you description it more sounds like you are giving them names to save the, small, effort of carrying them around in your loop. If this is the case I would put their pids in a record with all the other state data you carry around. This much better indicates the type of the processes.
如果您有一组固定的“订阅者”进程,则使用 IMO 注册名称。
相反,如果您有订阅者来来去去的发布/订阅类型的架构,那么您需要一个基础设施来跟踪这些订阅者,从这一点来看,使用 Pid() 还是名称并不重要。
使用注册名称的缺点之一是您需要在代码库中跟踪它们以避免“冲突”。所以这取决于你:就我个人而言,我倾向于支持命名进程,因为正如你所说,它使阅读代码更清晰。不管怎样,OTP 并不关心。
If you have a fixed set of "subscriber" processes, then use registered names IMO.
If, on the contrary, you have a publish/subscribe sort of architecture where subscribers come and go, then you need an infrastructure to track those and from this point it doesn't really matter if you use Pid() or names.
One of the drawbacks of using registered names is that you need to track them in your code base to avoid "collisions". So it is up to you: personally, I tend to favor named processes as, like you say, it makes reading the code clearer. One way or another, OTP doesn't care.