Requeuing a message in receive construct

Dominic Williams <>
Mon Apr 24 23:19:29 CEST 2006


Jérémie Lumbroso a écrit :

>
>  Hello,
>
>  I'm use the very practical receive ... end. construct in Erlang to
>  handle messages.
>
>  Everything is fine except that I would like an elegant way to delay
>  messages. I thought of keeping track of the bytes sent, and then
>  decreasing that amount every second. But I am unable to do this
>  elegantly. How do you periodly decrease that variable while still
>  maintaining that receive ... end. construct (or do you suggest
>  starting a process which's only task is to decrease X by a given
>  amount every second?).


I'm not sure what you're trying to achieve, but you could use 
timer:send_after/2 to resend yourself the message after the required 
delay, then receive selectively to handle that message first.

Another possibility is to use timer:apply_after/4 to do whatever you 
want to do with the message a bit later (or achieve the same thing by 
spawning, waiting, then processing.

spawn (
   fun() ->
      timer:sleep(...),
      process_message(...)
   end),
...

It depends on why you want to wait, and whether you can already 
determine exactly what you want to do later, or whether that could 
depend on messages that arrive in the meantime...

Hope that helps,

Dominic Williams
http://www.dominicwilliams.net

----



More information about the erlang-questions mailing list