[erlang-questions] Sending message at a specific and accurate time

Joe Armstrong erlang@REDACTED
Sun Feb 21 19:53:52 CET 2016


I want to send a UDP message at a specific time -

For each event I spawn a process


at_time_send(Host, Port, Time, Socket, Bin) ->
    %% io:format("at time ~p send",[Time]),
    spawn(fun() ->
                   send_message_at_time(Host, Port, Time, Socket, Bin)
                end).

The process sends a message to itself at the specified time

send_message_at_time(Host, Port, WantedTime, Socket, Bin) ->
    Tnow  = my_now(),
    Delta = trunc((WantedTime - Tnow)*1000),
    if
       Delta > 0 ->
          erlang:send_after(Delta, self(), ping),
          receive
             ping ->
               gen_udp:send(Socket, Host, Port, Bin)
          end;
       true ->
         void
      end.

my_now() ->
    %% seconds past epoch
    erlang:system_time()/1000000000.

I'm getting about a 4 - 9 ms. inaccuracy in the time the messages is sent and
the time I want it to be sent - but I'd like it to be much more accurate
(sub ms if possible)

Is there a more accurate way to do this than in my code? Should I just
have one process for all events (not one per event) - can I mess with
process priorities or command line flag to achieve better timing
accuracy?


Cheers

/Joe



More information about the erlang-questions mailing list