gen_server 启动了吗?
有没有办法告诉 gen_server:“主管已初始化所有 gen_server,现在您可以发送消息”?
我有一个工作人员 gen_server,其工作是在他的监督树中设置其他 gen_server 的状态。如果我刚刚开始在配置服务器的 init 函数中发送消息,有时它会收到 {noproc, _}。我想这意味着配置服务器太快了:他在主管有足够的时间启动所有工作人员之前发送了消息。我通过将timer:sleep(500)放入config_server:init()中修复了这个问题,这确保了所有gen_server有足够的时间来初始化,但这似乎是一个不优雅的解决方案。
有没有正确的方法来做到这一点?
Is there a way to tell a gen_server: "supervisor has initialised all gen_servers, now you can send then messages"?
I have a worker gen_server whose job is to set up states of other gen_servers in his supervision tree. If I just start sending messages in init function of my configuration server, sometimes it gets {noproc, _}. I suppose that means that config server was to fast: he sent messages before supervisor had enough time to start all workers. I fixed that by putting timer:sleep(500) in config_server:init(), which ensures all gen_server had enough time to initialise, but this seems like a inelegant solution.
Is there a proper way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从
init
返回超时0
的元组。然后在它返回后,handle_info(timeout, State)
将立即被调用。在handle_info
中进行一些调用,该调用在supervisor完成初始化之前不会返回(例如supervisor:which_children
)。Return tuple with timeout
0
frominit
. Then immediately after it returns,handle_info(timeout, State)
will be called. Inhandle_info
make some call which won't return until the supervisor finishes initialization (e.g.supervisor:which_children
).这是我处理这个问题的案例。该工作进程仅在被请求时才会被触发。
This is my case to handle this issue. This worker process is triggered only when it is requested.
在函数
init()
中调用gen_server:cast(init, State)
。消息“init”将位于消息队列中的第一个in function
init()
callgen_server:cast(init, State)
. message "init" will be first in message queue