यह संभावना है कि आप अनुक्रमिक के साथ तुल्यकालिक मिश्रण कर रहे हैं ।
एरलांग में एक समारोह के शरीर को क्रमिक रूप से संसाधित किया जा रहा है। तो स्पेंसर ने इस "ऑटोमैगिकल इफेक्ट" के बारे में क्या कहा, यह एरलांग के लिए सही नहीं है। आप इस व्यवहार को इरलांग के साथ जोड़ सकते हैं।
उदाहरण के लिए आप एक ऐसी प्रक्रिया को शुरू कर सकते हैं जो एक पंक्ति में शब्दों की संख्या की गणना करती है। जैसा कि हम कई पंक्तियाँ रखते हैं, हम प्रत्येक पंक्ति के लिए इस तरह की एक प्रक्रिया करते हैं और उससे एक राशि की गणना करने के लिए उत्तर प्राप्त करते हैं।
इस तरह, हम प्रक्रियाएँ करते हैं जो "भारी" संगणना (यदि उपलब्ध हो तो अतिरिक्त कोर का उपयोग) और बाद में हम परिणाम एकत्र करते हैं।
-module(countwords).
-export([count_words_in_lines/1]).
count_words_in_lines(Lines) ->
% For each line in lines run spawn_summarizer with the process id (pid)
% and a line to work on as arguments.
% This is a list comprehension and spawn_summarizer will return the pid
% of the process that was created. So the variable Pids will hold a list
% of process ids.
Pids = [spawn_summarizer(self(), Line) || Line <- Lines],
% For each pid receive the answer. This will happen in the same order in
% which the processes were created, because we saved [pid1, pid2, ...] in
% the variable Pids and now we consume this list.
Results = [receive_result(Pid) || Pid <- Pids],
% Sum up the results.
WordCount = lists:sum(Results),
io:format("We've got ~p words, Sir!~n", [WordCount]).
spawn_summarizer(S, Line) ->
% Create a anonymous function and save it in the variable F.
F = fun() ->
% Split line into words.
ListOfWords = string:tokens(Line, " "),
Length = length(ListOfWords),
io:format("process ~p calculated ~p words~n", [self(), Length]),
% Send a tuple containing our pid and Length to S.
S ! {self(), Length}
end,
% There is no return in erlang, instead the last value in a function is
% returned implicitly.
% Spawn the anonymous function and return the pid of the new process.
spawn(F).
% The Variable Pid gets bound in the function head.
% In erlang, you can only assign to a variable once.
receive_result(Pid) ->
receive
% Pattern-matching: the block behind "->" will execute only if we receive
% a tuple that matches the one below. The variable Pid is already bound,
% so we are waiting here for the answer of a specific process.
% N is unbound so we accept any value.
{Pid, N} ->
io:format("Received \"~p\" from process ~p~n", [N, Pid]),
N
end.
और यह वह है जो ऐसा दिखता है, जब हम इसे शेल में चलाते हैं:
Eshell V5.6.5 (abort with ^G)
1> Lines = ["This is a string of text", "and this is another", "and yet another", "it's getting boring now"].
["This is a string of text","and this is another",
"and yet another","it's getting boring now"]
2> c(countwords).
{ok,countwords}
3> countwords:count_words_in_lines(Lines).
process <0.39.0> calculated 6 words
process <0.40.0> calculated 4 words
process <0.41.0> calculated 3 words
process <0.42.0> calculated 4 words
Received "6" from process <0.39.0>
Received "4" from process <0.40.0>
Received "3" from process <0.41.0>
Received "4" from process <0.42.0>
We've got 17 words, Sir!
ok
4>