Skip to content

Instantly share code, notes, and snippets.

@jompa
Last active December 22, 2015 19:39
Show Gist options
  • Select an option

  • Save jompa/6521437 to your computer and use it in GitHub Desktop.

Select an option

Save jompa/6521437 to your computer and use it in GitHub Desktop.
Erlang processes
%%% Playing with processes and http requests
%%% c(requestr).
%%% request:start().
-module(requestr).
-export([start/0, pong/1, ping/2, traverse/2]).
pong(N) ->
receive
{pong, Time, Url, Status} ->
io:format("~p Status ~p in ~p microseconds~n", [Url, Status, Time]),
if
N > 0 ->
timer:sleep(1000),
pong(N-1)
end
end.
ping(Url, Pong_PID) ->
io:format("Pinging ~p~n", [Url]),
{Time, {ok, {{_Version, Status, _ReasonPhrase}, _Headers, _Body}}} = timer:tc(httpc, request, ["http://" ++ Url]),
Pong_PID ! {pong, Time, Url, Status}.
traverse([], _Pong_PID) ->
io:format("Traverse finished ~n", []);
traverse([Url|Urls], Pong_PID) ->
io:format("calling Ping with ~p~n", [Url]),
spawn(requestr, ping, [Url, Pong_PID]),
traverse(Urls, Pong_PID).
start() ->
Urls = ["www.svd.se", "www.dn.se", "www.aftonbladet.se", "www.google.com, "www.expressen.se", "www.nordea.se"],
Url_items = length(Urls),
inets:start(),
Pong_PID = spawn(requestr, pong, [Url_items]),
traverse(Urls, Pong_PID).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment