Multiple calls to a single function

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

Multiple calls to a single function

shiva gattu
Hi all, 
   
      If multiple gen_server processes are sending multiple messages to a single static function. The static function will execute all messages without loss of a single message ? 

     Is there any method to do that like using a pool? 

     My requirement is multiple processes will send multiple messages to one function, that function will send that messages to the web. 

What is a simple way to do this?

Regards,
Shiva


Reply | Threaded
Open this post in threaded view
|

Re: Multiple calls to a single function

Nalin Ranjan
Message are sent between processes.

To start with, have a process sending messages to the web, and make all gen_server(s) send their messages to this process, and then optimize. 

Also, there isn't any thing called static function(s) in ErLang. Not sure if you are referring to any other language. 

नमस्ते।
नलिन रंजन

On Tue, Apr 6, 2021, 12:20 PM shiva gattu <[hidden email]> wrote:
Hi all, 
   
      If multiple gen_server processes are sending multiple messages to a single static function. The static function will execute all messages without loss of a single message ? 

     Is there any method to do that like using a pool? 

     My requirement is multiple processes will send multiple messages to one function, that function will send that messages to the web. 

What is a simple way to do this?

Regards,
Shiva


Reply | Threaded
Open this post in threaded view
|

Re: Multiple calls to a single function

Torben Hoffmann
In reply to this post by shiva gattu
Hi Shiva,

By "single static function" I assume you mean a regular function in Erlang, right?

All functions in Erlang are executed in a process, each gen_server is a process, so if you simply call the function from within the gen_server it will be executed in that process.

You mention using a pool, so I sense that there is an additional requirement that you are not articulating.

Cheers,
Torben 

On Tue, 6 Apr 2021 at 08:50, shiva gattu <[hidden email]> wrote:
Hi all, 
   
      If multiple gen_server processes are sending multiple messages to a single static function. The static function will execute all messages without loss of a single message ? 

     Is there any method to do that like using a pool? 

     My requirement is multiple processes will send multiple messages to one function, that function will send that messages to the web. 

What is a simple way to do this?

Regards,
Shiva




--
Reply | Threaded
Open this post in threaded view
|

Re: Multiple calls to a single function

zxq9-2
In reply to this post by shiva gattu
Hi, Shiva.

On 2021/04/06 15:26, shiva gattu wrote:
>        If multiple gen_server processes are sending multiple messages to
> a single static function. The static function will execute all messages
> without loss of a single message ?

Function calls are not the same as message passing. Processes pass
messages between one another, and each process can call any function
within its own context without interfering with any other. Processes are
analogous to Java threads -- a dozen threads might call the same
function and they don't interfere with each other unless they access a
shared data reference. In Erlang the data space between process is
*completely* isolated, so two processes cannot access the same data at all.

I wrote a little thing about this comparing Java threads and Erlang
processes you might find helpful -- thinking the Erlang way is a much
more significant difference than the relatively minor difference between
OOP and FP:
https://stackoverflow.com/questions/32294367/erlang-processes-vs-java-threads/32296577#32296577

>       Is there any method to do that like using a pool?

Pooling in Erlang occurs at the process level, not the function level.
It is the same idea as having a pool of worker threads or a pool of
database connections: a set of worker processes that are already spawned
and loaded with some necessary state. Erlang process spawning is
remarkably cheap in terms of memory and processing time, so if there
isn't much overhead involved with populating a worker's state or if each
worker isn't tied to an external resource like a file or network
connection that needs to remain open, it isn't a big deal to just spawn
the workers you need when you need them. Figuring out when pooling is
*really* needed (not actually very often, but it is a very popular
pattern because it is comfortable) VS when it isn't actually buying you
anything takes some experience with Erlang and the runtime and often a
bit of testing and observation of your program under real-world loads.

>       My requirement is multiple processes will send multiple messages
> to one function, that function will send that messages to the web.

I think you may be conflating the idea of "function" and "process". If
you have a single process that owns a connection to something on the web
and you want to pass lots of messages through it that come from
different processes within your side of the system, this is totally
possible. The messages simply arrive in that process's mailbox (a FIFO
queue managed by the runtime) and it will react to them in order.

That's all there is to it.

We usually abstract the actual act of sending a message *behind* a
function call that is defined on the target process's module code -- but
those interface functions are being *called* in the context of the
process that is sending, not the one receiving. This takes a little bit
of time to make clear in the mind if you're new to Erlang.

> What is a simple way to do this?

Just do it! Have your worker processes all send messages to the same
process and that's it.

Without seeing a little code or getting a bit more context information
about your specific situation it is hard to say more than this, but you
may have questions based on the above -- so feel free to ask. Erlang is
a lot of fun once it starts all making sense.

-Craig