gproc
gproc copied to clipboard
Question on loading new/stale property with get_env
I am using get_env() to read a config file (i.e. [app_env]).
If I change the configuration file, is there an API I can use to tell gproc to reload this (or all) properties? It's not exactly clear that there is a way, and I'm digging into the code right now to figure it out.
thanks
Well, if you use gproc:get_env() consistently, gproc will always go back to the source. If you use gproc:get_set_env(), gproc will cache the result (even undefined if the value isn't found).
Once you have cached the value, gproc will read the cache every time. There is no API function for clearing the cache, but you could do:
Eshell V8.0 (abort with ^G)
1> application:ensure_all_started(gproc).
{ok,[gproc]}
2> application:set_env(gproc,a,5). % load conf
ok
3> gproc:get_set_env(l,gproc,a). % cache value
5
4> {gproc,L} = gproc:info(self(),{gproc,{p,l,{gproc_env,'_','_'}}}).
{gproc,[{{p,l,{gproc_env,gproc,a}},5}]}
5> [gproc:unreg(K) || {K,V} <- L]. % clear cache
[true]
6> gproc:info(self(),{gproc,{p,l,{gproc_env,'_','_'}}}).
{gproc,[]}
7> application:set_env(gproc,a,7). % reload conf
ok
8> gproc:get_set_env(l,gproc,a).
7
9> gproc:info(self(),{gproc,{p,l,{gproc_env,'_','_'}}}).
{gproc,[{{p,l,{gproc_env,gproc,a}},7}]}
The pattern gproc:info(Pid, {gproc, Pattern}) is not documented. This is an oversight. It should be the most scalable[1] way to fetch your own cached values (remember, you can't clear the cache of any other process).
[1] I.e. it uses the reverse mapping for the Pid. The QLC table generator could theoretically do that too, if the pid pattern is bound, but it currently doesn't. Anyone who feels strongly about this is welcome to try for a PR. Using gproc:select() with wildcards in the gproc_env tuple pattern, the operation will scan the cached values of all processes.