[erlang-questions] Beginner gen_server and child monitoring

Torben Hoffmann thoffmann@REDACTED
Tue Aug 2 15:33:24 CEST 2016


Hi Giovanni,

Giovanni Giorgi <jj@REDACTED> writes:

> Hi all,
>  I am an erlang newbies.
> I am developing a small stock exchange monitoring application, after reading Francesco Cesarini Erlang book and wpriting some small libraries:
>
> https://github.com/daitangio/erprice
>
> I have a gen_server which take a monitor request, in the form {StockTicker, Market, droplimitvalue}.
> Then the gen_server spawn a child process which track the price, sleeping at some interval.
> The child process is monitored.
> The child process die when the stock goes below the price drop but before that sends a notification to its gen_server
>
> Now the question is: Is a correct approach?
> What is the best way of spawn-ing child which must loop and loop?
>

It depends on how much of a handle on the spawned processes you want.

Your solution makes it a little hard to interogate the children because
they are just spawned as simple processes.

> I think the child cannot be a gen_server because its interface did not
> seem to be thinked for continuing monitoring process.

You could use gen_server, but it is rather heavy for this purpose.

> There is some idiomatic behavior must I implement?
> I can spawn_monitot child by hand? It is a good strategy?

You could make the child an OTP-compliant process by following this
instruction:
http://erlang.org/doc/design_principles/spec_proc.html

The section on "Special Processes".

Then you can hook them into a supervision tree and use the sys module to
ask them what they are doing.

Small code style hint: when you do a gen_server:call/2 you should tag
the message in some way, e.g.,
   {spawned, NewPid}=gen_server:call(GenServer,{start_tick_tracker, Company, Market,lessthen,Quote}),
   
That makes it easier to figure out what the code in handle_call is for.

Cheers,
Torben

> --
> Giovanni Giorgi
> jj@REDACTED
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-- 
Torben Hoffmann
Chief Architect, basho.com
M: +45 25 14 05 38



More information about the erlang-questions mailing list