Skip to content

Instantly share code, notes, and snippets.

@zentrope
Created August 25, 2010 17:21
Show Gist options
  • Save zentrope/549898 to your computer and use it in GitHub Desktop.
Save zentrope/549898 to your computer and use it in GitHub Desktop.
-module(par).
-compile(export_all).
%% Just some experiments in parallel processing.
test_job() ->
Parent = self(),
spawn(fun() ->
do_job(Parent, {job, make_ref()})
end),
receive
Msg ->
Msg
end.
gen_jobs(Num) ->
[{job, Val} || Val <- lists:seq(1, Num) ].
spawn_jobs(Jobs) ->
Parent = self(),
Pids = [Pid || Pid <- [spawn(fun() -> do_job(Parent, Job) end) || Job <- Jobs]],
gather_jobs(Pids).
gather_jobs([]) ->
[];
gather_jobs([_Pid|Rest]) ->
Result = receive
Msg ->
Msg
end,
[Result] ++ gather_jobs(Rest).
do_job(Pid, Job) ->
{job, Num} = Job,
Millis = Num rem 4 * 1000,
io:format("sleeping for ~w~n", [Millis]),
sleep(Millis),
Pid ! {result, Job}.
sleep(Millis) ->
receive
after
Millis ->
done
end.
@zentrope
Copy link
Author

This was a tiny script I wrote to experiment to explore a "map/reduce" kind of paradigm for getting work done. I don't think I ever used anything like this, though, as I've not really had any problems needing this kind of solution. Still, I love how well you can prototype distributed solutions with Erlang. I bet you can do the same with Scala's actor implementation as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment