How to create an event emitter with elixir, the otp way -


what best way in elixir create foreground process tick on every given amount of time?

my main problem approach like:

defmoulde ticker   def tick     do_something()     :timer.sleep(1000)     tick   end end 

works, wrong design. it's not ticking every second, every second plus time do_something() complete. spawn process handle "something" still there small lag.

also, i'm trying build mix app, there genservers involved, , main foreground process (the 1 i'm asking here) should call servers every x seconds. there otp way of doing this?

i think timer:apply_interval/4 should suit needs. used this:

defmodule tick   def tick     io.inspect(:tick)   end end  :timer.apply_interval(:timer.seconds(1), tick, :tick, []) 

the arguments in order interval, module name, function call , arguments call with. can send messages genservers inside function. check out full documentation here: apply_interval/4

another thing consider if tick function ever sending simple messages genservers extremely fast, implementation might ok, once wrapped in task , plugged supervision tree (if don't care potential slight drift is).


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -