gen_fsm 初始状态,在生成时向其发送事件

发布于 2024-10-01 18:33:02 字数 115 浏览 0 评论 0原文

如果我想在生成 gen_fsm 时始终将事件发送到 gen_fsm 的初始状态,那么我应该将该函数调用放在哪里?紧接在 start_link 之后或从首先调用 start_link 的进程开始。这里有什么最佳实践吗?

If I want to always send an event to the initial state of a gen_fsm when I have spawned it, where should I put that function call? Right after start_link or from the process that invoked start_link in the first place. Are there any best practices here?

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

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

发布评论

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

评论(2

女皇必胜 2024-10-08 18:33:02

如果您只想在启动后更改 FSM 的状态,您可以简单地为状态机实现 init 函数:

阅读自: http://www.erlang.org/doc/man/gen_fsm.html#Module:init-1

每当 gen_fsm 开始使用
gen_fsm:start/3,4 或
gen_fsm:start_link/3,4,这个函数
被新进程调用
初始化。

Args 是提供给的 Args 参数
启动函数。

如果初始化成功,
函数应该返回
{ok,状态名称,状态数据},
{ok,StateName,StateData,Timeout} 或
{ok,状态名称,状态数据,休眠},
其中 StateName 是初始状态
name 和 StateData 初始状态
gen_fsm的数据。

另外,使用 init 函数,您可以确定两个函数(start_link 和 init)的原子性。他们都会成功或失败。

If you just want to alter the state of the FSM after you start it, you might simply implement the init function for your state machine:

Reading from: http://www.erlang.org/doc/man/gen_fsm.html#Module:init-1

Whenever a gen_fsm is started using
gen_fsm:start/3,4 or
gen_fsm:start_link/3,4, this function
is called by the new process to
initialize.

Args is the Args argument provided to
the start function.

If initialization is successful, the
function should return
{ok,StateName,StateData},
{ok,StateName,StateData,Timeout} or
{ok,StateName,StateData,hibernate},
where StateName is the initial state
name and StateData the initial state
data of the gen_fsm.

Also, using the init function, you're sure about the atomicity of the two functions (start_link and init). They will both succeed or fail.

很糊涂小朋友 2024-10-08 18:33:02

我认为从调用 FSM 启动函数的进程中发送第一个事件是正确的。或者从 init/1 返回 timeout = 0 并在初始状态下处理“timeout”事件。

另一方面,如果您的 gen_fsm 是一个注册进程,则它使竞争成为可能。如果是这种情况,我会在注册之前从 init/1 向 gen_fsm 进程 PID 发送消息。

I thik it is right to send first event from the process invoking FSM start function. Or return timeout = 0 from init/1 and handle 'timeout' event in the initial state.

On the other hand, it makes races possible if your gen_fsm is a rgistered process. If that is the case I would send message to the gen_fsm process PID from init/1 befor registering.

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