سؤال

My task is to process files inside a zip file. So I write bunch of independent functions and compose them to get the desired result. That's one way of doing things. Now instead of having it all written as functions, I write some of them as processes with selective receives and all, and every things is cool. But then pondering on this a bit further, I'm thinking like, do we need functions at all? Couldn't I replace or convert all those functions into processes that communicates to itself and to other processes? So there lies my doubt. When to use functions and when to use processes? Is there any advantage from performance standpoint in using functions (like caching)? Doesn't code blocks in the processes get cached similarly?

So in our example what's the standard idiom to proceed with? Current pseudo code below.

start() ->
  FL = extract("..path"),
  FPids = lists:map(open_file, FL), %  get file Pids,
  lists:foreach(fun(FPid) ->
                  CPid = spawn_compute_process(),
                  rpc(CPid,{compute,FPid}) 
                end, FPids).

compute() ->
  receive
    {Pid,{..}} ->
      Line = read_line(..),
      TL = tidy_line(Line), % an independent function. But couldn't it be a guard within this process?
    ..
  end.

extract(FilePath) -> FilesList.

read_line(FPid) -> line.  

So how do you actually write code? Like, write smaller independent functions first and then wrap them up inside processes?
Thanks.

هل كانت مفيدة؟

المحلول

The short answer is that you use processes to exploit concurrency. Replacing functions with processes where you sequentially run one process, then send its value to another process which then does its work and sends its result to the next process etc each process terminating after its done its bit is the wrong use of processes. Here you are just evaluating something sequentially by sending data from one process to another instead of calling functions.

If, however, you intend this chain of processes to be able to process multiple sequences of "calls" concurrently then it is a different matter. Then you are using the processes for concurrency. The more general way of doing this in erlang is to create a separate process for each sequence and exploit the concurrency in that manner.

Another use of processes is to manage state.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top