Powered by AppSignal & Oban Pro

Task Drills

exercises/task_drills.livemd

Task Drills

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue Task SupervisorConcurrent Word Count

Task Drills

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.

Task.await/2

Use Task.async/1 and Task.await/2 to spawn a task process that returns any response.

Use Task.async/1 and Task.await/2 to spawn a task process that sleeps for six seconds. It should cause a timeout error.

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.

Task.yield/2

Use Task.async/1 and Task.yield/2 to spawn a task process that returns any response.

Slow Operation

# 1000 Operations
{time, _result} = :timer.tc(fn -> Enum.map(1..10, fn int -> Process.sleep(1000) end) end)
time
{time, _result} =
  :timer.tc(fn ->
    # 1000 tasks
    # 1000 operations -> parallel
    tasks =
      Enum.map(1..10, fn int ->
        Task.async(fn ->
          IO.puts("starting task #{int}")
          Process.sleep(1000)
        end)
      end)

    Task.await_many(tasks)
  end)

10_009_225
1_001_494

Fast Operation

# 1000 Operations
{time, _result} = :timer.tc(fn -> Enum.map(1..10, fn int -> int * 2 end) end)
time
{time, _result} =
  :timer.tc(fn ->
    # 1000 tasks
    # 1000 operations -> parallel
    tasks = Enum.map(1..10, fn int -> Task.async(fn -> int * 2 end) end)
    Task.await_many(tasks)
  end)

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.

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.

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.

Task.await_many/2

Use Task.async/1 and Task.await_many/2 to spawn two task processes that both return a random number from 1..10.

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.

Use Enum.map/2, Task.async/1, and Task.await_many/2 to spawn one hundred task processes that return a list doubled numbers from one to one hundred. i.e. [2, 4, 6, 8, ..., 198, 200].

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"]

Task.Supervisor

Start a Supervisor with a child Task.Supervisor process named MyTaskSupervisor. You will use MyTaskSupervisor in all of the examples below that need a task supervisor.

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.

Use Enum.map/2, Task.Supervisor.async/3 and Task.await_many/2 to start one hundred supervised task processes that returns a list of doubled numbers from one 1..100. i.e. [2, 4, 6, 8, ..., 198, 200].

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.

Use Task.Supervisor.start_child/3 with the restart: :transient option to start a :transient task that crashes after five seconds.

Use Supervisor.which_children/1 after six seconds to demonstrate that the :transient process is restarted.

Commit Your Progress

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.

Navigation

Home Report An Issue Task SupervisorConcurrent Word Count