Created
November 10, 2014 10:32
-
-
Save benjamintanweihao/4f9b9466fd75becadee9 to your computer and use it in GitHub Desktop.
Transforming from non-distributed ... to distributed.
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
| # Non-distributed | |
| defp do_requests(url, n_requests) do | |
| coord_task = Task.async(Coordinator, :start, [n_requests]) | |
| Enum.map(1..n_requests, fn i -> | |
| spawn(Worker, :start, [url, i]) | |
| end) | |
| Task.await(coord_task, :infinity) |> parse_results | |
| end | |
| # Distributed | |
| defp do_requests(url, n_requests, nodes) do | |
| total_nodes = Enum.count(nodes) | |
| requests_per_node = div(n_requests, total_nodes) | |
| tasks = nodes |> Enum.map(fn node -> | |
| Task.Supervisor.async({:coord_tasks_sup, node}, Coordinator, :start, [requests_per_node]) | |
| end) | |
| # this function needs to run on all the nodes. | |
| Enum.each(nodes, fn node -> | |
| Enum.each(1..requests_per_node, fn i -> | |
| Node.spawn(node, Worker, :start, [url, i]) | |
| end) | |
| end) | |
| # Await for all the coordinator tasks | |
| Enum.map(tasks, fn task -> | |
| Task.await(task, :infinity) |> parse_results | |
| end) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment