[erlang-questions] automatically delete older messages with the same tag (atom)?

Bengt Kleberg bengt.kleberg@REDACTED
Wed May 6 08:36:42 CEST 2009


Greetings,

An alternative would be to use an ETS
(http://erlang.org/doc/man/ets.html) table of type 'set'.


bengt

On Wed, 2009-05-06 at 11:23 +1200, Cameron Kerr wrote:
> You just need a small proxy process that implements a message
> dictionary rather than a message queue. The dictionary could even be
> the processes dictionary. Something like this perhaps (completely
> untested, and from a relative newbie)
> 
> 
> -module(message_dictionary).
> -export([get/2, start/0, stop/1]).
> 
> 
> start() ->
> spawn(fun loop/0).  % should probably be spawn_link instead
> 
> 
> 
> loop() ->
> receive
> 
> {get, From, Tag} ->
> 
> From ! erlang:get(Tag),
> 
> io:format("Returning message with tag ~p, value ~p and removing from
> dictionary~n",
> 
> [Tag, erlang:get(Tag)]),
> 
> erlang:erase(Tag),
> 
> loop();
> 
> stop ->
> ok;
> 
> {Tag, Msg}=M ->
> case erlang:put(Tag, Msg) of
> undefined ->
> io:format("Received message ~p; did not replace any old message~n",
> [M]);
> Old ->
> 
> io:format("Received message ~p; replaced old message ~p~n", [M, Old])
> 
> end,
> 
> loop();
> Other ->
> io:format("Received untagged message ~p~n", [Other]),
> loop(); 
> 
> end.
> 
> 
> %%% Client functions
> 
> 
> get(Pid, Tag) ->
> Pid ! {get, self(), Tag},
> 
> receive
> 
> Any -> Any
> 
> end.
> 
> 
> 
> stop(Pid) ->
> Pid ! stop.
> 
> 
> 
> 
> 
> So basically all it does is implement a small process which accepts
> messages (eg. {msgtag, {some, [structure, of], [[whatever]]}}) and
> stores this in the processes dictionary, as illustrated with just put
> and get below:
> 
> 
> 1> put(foo, bar).
> undefined
> 2> put(foo, baz).
> bar
> 3> get(foo).
> baz
> 
> 
> Hopefully there are no glaring faults; I'm still reasonably new to
> designing programs in Erlang myself, perhaps others will help to point
> out and repair any holes.
> 
> 
> On 06/05/2009, at 3:04 AM, Giorgos Kollias wrote:
> 
> > Hello Erlang users,
> > 
> > Is there an option in the Erlang runtime to automatically delete a
> > message that has been deposited into a process's mailbox as soon as
> > a newer message with the same tag arrives? Is it easy to "patch" the
> > Erlang language source code to support this "little" feature in case
> > this is not there?
> > 
> > Or the only feasible way of getting this behavior is just to receive
> > all messages till you get a sort of timeout and just keep the latest
> > message with the tag you are interested in (i.e. user code only) in
> > each receive phase? Is there a standard code idiom/snippet for this?
> > 
> > This feature would be very interesting for implementing the
> > asynchronous iterations computing paradigm where each process only
> > keeps the most up-to-date message when it looks into its incoming
> > messages. 
> > 
> > Thanks in advance,
> > 
> > giorgos
> > 
> > 
> > 
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> 
> -- 
> Cameron Kerr <ckerr@REDACTED>
> Teaching Fellow, Computer Science, University of Otago
> 
> 
> 
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list