[erlang-questions] Question about gproc and "wildcards"

Ulf Wiger ulf.wiger@REDACTED
Thu Apr 28 23:12:12 CEST 2011


Hi Greg,

I hadn't really thought of it before, but perhaps something like this:

Eshell V5.8.4  (abort with ^G)
1> application:start(gproc).
ok

2> Loop = fun(L) -> receive M -> io:fwrite("~p <- ~p~n", [self(),M]) end, L(L) end.
#Fun<erl_eval.6.13229925>

3> P1 = spawn(fun() -> gproc:reg({p,l,my_event}, [a,b]), Loop(Loop) end).
<0.40.0>

4> P2 = spawn(fun() -> gproc:reg({p,l,my_event}, any), Loop(Loop) end).
<0.42.0>

5> Notify = fun(X) -> qlc:e(qlc:q([P ! {my_event,X} || {{p,l,my_event},P,V} <- gproc:table(props), V==any orelse (is_list(V) andalso lists:member(X,V))])) end.
#Fun<erl_eval.6.13229925>

6> Notify(a).
<0.40.0> <- {my_event,a}
<0.42.0> <- {my_event,a}
[{my_event,a},{my_event,a}]

7> Notify(c).
[{my_event,c}]
<0.42.0> <- {my_event,c}

BR,
Ulf


On 28 Apr 2011, at 22:05, Gregory Haskins wrote:

> Greetings Ulf, all,
> 
> Question about gproc.  I am using gproc in a variety of ways, one of
> which is as a pub/sub mechanism as advertised in the readme.md (see:
> https://github.com/ghaskins/edist/blob/master/lib/src/main/erlang/edist_event_bus.erl
> as an example).
> 
> What I am wondering is, is there a way to "subscribe" to something with
> wildcards?
> 
> For instance, can I say "subscribe({interesting_event, '_'})" such that
> I would get notifications for
> 
> notify({interesting_event, foo})
> 
> as well as
> 
> notify({interesting_event, bar})
> 
> I am a bit unclear if or how this could be done.
> 
> Thanks!
> -Greg
> 

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com






More information about the erlang-questions mailing list