Tasks

  • Extends spawn to provide better error & introspection

  • Meant to execute on task with little communication, like turning seq code into async to compute values/send requests

Methods Overview

  • Task.start/1

  • Task.start_link/1

  • Task.async/1 - expects reply that is always sent (will crash if caller crashes)

  • Task.await/1

Sequential => Concurrent Code

task = Task.async(fn -> do_some_work() end)
res = do_some_other_work()
res + Task.await(task)

Adhoc Tasks

Can easily run without a supervisor for unessential tasks like emails

Task.start(fn -> send_email_to_user(user) end) # 🎉

Async requests

Task Supervisor

Summary:

Setup

Application.ex

Somewhere in app

account_setup.ex

Tasks will be run with when that ft

Your CRM sample

Last updated