Last active
December 22, 2015 19:39
-
-
Save jompa/6521437 to your computer and use it in GitHub Desktop.
Erlang processes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %%% 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