Multiple calls to a single function
zxq9
zxq9@REDACTED
Tue Apr 6 15:25:48 CEST 2021
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
More information about the erlang-questions
mailing list