Is a supervisor necessary here/when should I use one?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is a supervisor necessary here/when should I use one?

asdf asdf
Hello,

I have an Erlang server running on my system(emqtt), and I created a plugin for it. In this plugin, I have one function that I would like to run once on startup called startup() that passes the next function to the environment as a function reference and is used thousands of times a minute (ideally). My function simply searches a database for a key and returns a value.

The function that is run hundreds of times a minute does not wait around with a receive though. I am reading the textbook “learn you some Erlang for good,” and all of the “workers” described are constantly waiting on messages(receive) and performing actions.  This function is just a function though… like if you had a function 2+2, it is simple.

So, when I enable the plugin and the supervisor starts, my startup() function is run properly, but exits and error text is displayed after completing properly because the worker is not, well, doing anything until it is started to be called.

Conceptually, I am new to this. Is there any place for a supervisor here? My goals are to run the startup script that just sets the function in the environment and to optimize my function speed. If there is no place for a supervisor, how would I run my Erlang startup program without having the console freak out at me?

Thank you for your help!
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Is a supervisor necessary here/when should I use one?

zxq9-2
On 2017年07月21日 金曜日 15:35:48 code wiget wrote:
> Conceptually, I am new to this. Is there any place for a supervisor here? My goals are to run the startup script that just sets the function in the environment and to optimize my function speed. If there is no place for a supervisor, how would I run my Erlang startup program without having the console freak out at me?

Functions don't have supervisors, processes do.

Think about processes as workers in your information factory.

Think about functions as things your workers do -- and ANY worker may perform ANY function (potentially).

With this mental image in mind answer the question:
Which process is calling this function?

The answer will tell you the context of the execution. The context of execution will answer the rest of your questions as a matter of course (and if not, you will automatically solicit many experienced opinions here if you answer the question above publicly in this thread -- and especially if you continue to brainstorm by typing things out here).


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Is a supervisor necessary here/when should I use one?

Vance Shipley
In reply to this post by asdf asdf
On Sat, Jul 22, 2017 at 1:05 AM, code wiget <[hidden email]> wrote:
> So, when I enable the plugin and the supervisor starts, my startup() function is run properly, but exits and error text is displayed after completing properly because the worker is not, well, doing anything until it is started to be called.

I always start with a proper supervision hierarchy for all processes
following the old Erlang adage "optimize last, if ever". It's true
that with high frequency starting of children supervision introduces
an overhead which could be a significant portion of overall
load/latency however the stability provided should not be
underestimated. In these cases you will want to configure the sasl
application to suppress printing progress reports which would
otherwise make the console/logs unusable so add this to your
sys.config file:

     {sasl, [{errlog_type, error}]}

> Conceptually, I am new to this. Is there any place for a supervisor here?

IMHO there is always a use for supervision. One thing I pay attention
to is what happens when you perform an orderly shutdown. Start your
application and sasl then type "q()." in the console and you should
get a silent exit within four seconds (typically). If you get reams of
reports or hang a long time things aren't stable.

--
     -Vance
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions