Process Drills
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Process 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 the Process module. Follow the instructions for each drill and complete them as quickly as you can.
Process.send/3
Use Process.send/3 and self() to send the process for the Elixir cell below a :message message. Use receive to receive the message in the same cell.
Use Process.send/3 and self() to send the process for the Elixir cell below a message with a value i.e. {:message, "value"}. Use receive to receive the message in the same cell and return the value.
Process.spawn/2
Use Process.spawn/2 to spawn a new process which adds two integers together.
Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds, then prints “Finished!”.
Use Process.spawn/2 and receive to spawn a process that receives a :message message. Use Process.send/3 to send the spawned process a :message message. The spawned process should print "received a message!".
Use Process.spawn/2 and receive to spawn a process that receives a message with a value i.e. {:message, "value"}. Use Process.send/3 to send the spawned process a message with a value. The spawned process should print the received value.
Use Process.spawn/2 to spawn a process that raises an error. Notice it does not crash the Livebook, because it is an unlinked process.
Use Process.spawn/3 and Process.sleep/1 to spawn a process which raises an error after one second. Use Process.link/1 to link the process.
Livebook should crash. Comment out your solution so that you can move on.
Process.alive?/1
Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds. Use Process.alive?/1 and Process.sleep/1 to check if the process is alive after two seconds. Process.alive?/1 should return true.
Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds. Use Process.alive?/1 and Process.sleep/1 to check if the process is alive after six seconds. Process.alive?/1 should return false.
Process.send_after/4
Use Process.send_after/4 and self() to send the process for the Elixir cell below a message after two seconds. Use receive in the same cell to receive the message.
Use Process.spawn/3 to and receive to spawn a process that waits to receive a message. Use Process.send_after/4 to send the spawned process a message after two seconds.
Process.exit/2
Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds. Print "I started" before sleeping, and "I finished" after sleeping to prove if the process finishes sleeping or not. Use Process.exit/2 with the :normal exit reason to kill the spawned process.
Once finished, switch the exit reason to :kill to demonstrate that the process is unlinked, because it does not crash the calling process.
Use Kernel.spawn_link/1 and Process.sleep/1 to spawn a linked process that sleeps for five seconds. Use Process.exit/2 with the :normal exit reason to kill the spawned process.
Notice that the Livebook does not crash. Switch the exit reason to :kill and notice that the Livebook does crash. Comment out your solution to avoid crashing Livebook.
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "process_drills_reading"
"exercises" -> "process_drills_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(
progress_path,
Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout -b process-drills-exercise
$ git add .
$ git commit -m "finish process drills exercise"
$ git push origin process-drills-exercise
Create a pull request from your process-drills-exercise branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.
Up Next
| Previous | Next |
|---|---|
| Processes | Process Mailbox |