Is there any Erlang behavior that can act on its own and not wait to be called?

I am writing an Erlang application that requires active polling of some remote resources, and I want the process that does the polling to fit into the OTP monitoring trees and support all standard tools such as terminating correctly, reloading hot code, etc.

However, two default behaviors, gen_serverand gen_fsmseem to support callback based operation. I could abuse gen_serverto do this through self calls or abuse gen_fsm, having one state that always loops on itself with a timeout of 0, but I'm not sure if it is safe (i.e. does not exhaust the stack or accumulate unread messages in the mailbox )

I could make my process a special process and write everything that I processed myself, but it actually makes me redefine the Erlang wheel equivalent.

So is there any behavior for code like this?

loop(State) ->
  do_stuff(State), % without waiting to be called
  loop(NewState).

And if not, is there a safe way to trick default behavior into this without exhausting the stack or accumulating messages over time or something like that?

+3
source share
2 answers

The standard way to do this in Erlang is with erlang:send_after/3. See This SO answer as well as this sample implementation .

+3
source

, OTP-? OTP, , gen_server gen_fsm, , .

, , , gen_server, gen_server ( active_poll), , ( - , ), active_poll , gen_server, , gen_server, . , , , - gen_server code_change, , active_poll, , - .

- / gen_server/gen_fsm/Similar ...

, " " . , OTP, , - sys, , gen_server/gen_fsm ( , , ).

-, , (, , , ), , gen_server, gen_fsm ( , , , gen_server/gen_fsm , , receive, sys ).

, , , , - , ( , -, , , , , - hog). 1 , , . , , . - ( , ), , , .

+1

Source: https://habr.com/ru/post/1629047/


All Articles