Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Drills help you develop familiarity and muscle memory with syntax through repeated exercises. Unlike usual problems, Drills are not intended to develop problem solving skills, they are purely for developing comfort and speed.
This set of drills is for Tasks. Follow the instructions for each drill and complete them as quickly as you can.
Use Task.async/1 and Task.await/2 to spawn a task process that returns any response.
Example Solution
task = Task.async(fn -> :ok end)
Task.await(task)
Use Task.async/1 and Task.await/2 to spawn a task process that sleeps for six seconds. It should cause a timeout error.
Example Solution
task = Task.async(fn -> Process.sleep(6000) end)
Task.await(task)
Use Task.async/1 and Task.await/2 to spawn a task process that sleeps for six seconds. Provide a timeout value to Task.await/2 so that it properly awaits the response.
Example Solution
task = Task.async(fn -> Process.sleep(6000) end)
Task.await(task, 7000)
Use Task.async/1 and Task.yield/2 to spawn a task process that returns any response.
Example Solution
task = Task.async(fn -> :ok end)
Task.yield(task)
Use Task.async/1 and Task.yield/2 to spawn a task process that sleeps for six seconds and returns any response. Notice that the yield is nil
.
Example Solution
task = Task.async(fn -> Process.sleep(6000) end)
Task.yield(task)
Use Task.async/1 and Task.yield/2 to spawn a task process that sleeps for six seconds and returns any response. Provide a timeout value to Task.yield/2 so that it properly returns the response.
Example Solution
task = Task.async(fn -> Process.sleep(6000) end)
Task.yield(task, 7000)
Use Task.async/1, Task.yield/2, and Task.shutdown/2 to spawn a task process that sleeps for six seconds. Shutdown the task process if it does not yield any value after five seconds.
Use IO.puts/2
to prove that the task was shutdown.
Example Solution
task =
Task.async(fn ->
Process.sleep(6000)
IO.puts("NOT RAN")
end)
if !Task.yield(task) do
Task.shutdown(task)
end
Use Task.async/1 and Task.await_many/2 to spawn two task processes that both return a random number from 1..10
.
Example Solution
task1 = Task.async(fn -> Enum.random(1..10) end)
task2 = Task.async(fn -> Enum.random(1..10) end)
Task.await_many([task1, task2])
Use Enum.map/2, Task.async/1, and Task.await_many/2 to spawn one hundred task processes that all return a random integer from 1..100
.
Example Solution
tasks = Enum.map(1..100, fn _ -> Task.async(fn -> Enum.random(1..100) end) end)
Task.await_many(tasks)
Use Enum.map/2, Task.async/1, String.upcase/2, and Task.await_many/2 to concurrently capitalize every string in the list ["apple", "pear", "peaches"]
Example Solution
list = ["apple", "pear", "peaches"]
tasks = Enum.map(list, fn word -> Task.async(fn -> String.upcase(word) end) end)
Task.await_many(tasks)
Start a Supervisor with a child Task.Supervisor
process named MyTaskSupervisor
. Use a :one_for_one
strategy. You will use MyTaskSupervisor
in all of the examples below that need a task supervisor.
Example Solution
children = [
{Task.Supervisor, name: MyTaskSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
Use Task.Supervisor.async/3
and Task.await/2 to start a supervised task process that returns a random number after sleeping for two seconds.
Example Solution
task = Task.Supervisor.async(MyTaskSupervisor, fn ->
Process.sleep(2000)
Enum.random(1..10)
end)
Task.await(task)
Use Enum.map/2, Task.Supervisor.async/3
and Task.await_many/2 to start one hundred supervised task processes that returns a random number.
Example Solution
tasks = Enum.map(1..100, fn _ ->
Task.Supervisor.async(MyTaskSupervisor, fn -> Enum.random(1..10) end)
end)
Task.await_many(tasks)
Use Task.Supervisor.start_child/3
to start a :temporary
task that crashes after five seconds. :temporary
is already the default :restart
option.
Use Supervisor.which_children/1 after six seconds to demonstrate that a :temporary
process is not restarted.
Example Solution
task =
Task.Supervisor.start_child(MyTaskSupervisor, fn ->
Process.sleep(5000)
raise "error"
end)
Process.sleep(6000)
Supervisor.which_children(MyTaskSupervisor)
Use Task.Supervisor.start_child/3
with the restart: :transient
option to start a :transient
task that crashes after one second.
Use IO.inspect/2
and self/0
to demonstrate that the process is restarted, and that the pid
has changed.
Example Solution
task =
Task.Supervisor.start_child(
MyTaskSupervisor,
fn ->
IO.inspect(self(), label: "Process restarted")
Process.sleep(1000)
raise "error"
end,
restart: :transient
)
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Task Drills exercise"
$ git push
We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.