josefrichter
TLDR: what’s the difference between Task.Supervisor.async_nolink and Taks.start, and which one I should call in Phoenix controllers, please?
Full story:
I have read several threads here and several blogposts on using simple Tasks, and would like to solidify my understanding
I understand you shouldn’t use Task.async if you don’t need to Task.await.
In those situations, it seems like I should use either Task.Supervisor.async_nolink which has the benefit that it’s not linked to process that started it (in my case e.g. the phoenix controller process that dies too soon and would take down this task with it), but you are still able to supervise it and handle failures if I got that right. (For which you’d need a GenServer to handle that?).
Another option seems to be Task.start which docs say only used when the task is used for side-effects (i.e. no interest in the returned result) - so is this one the truly simplest fire & forget? Not linked with anything, no idea whether it succeeds or not, not particularly important?
Example of my specific use case: I have a simple todo list, and when users check any of the todos, I want to run a background check whether all his todos are finished and send an email if yes - all of this can happen in the background without the user knowing, maybe only me as admin should see the failure somewhere in logs.
Just a note: I am not looking for a library, which probably exists. I’d like a pure elixir solution in order to learn and understand first.
Thank you!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wojtekmach
Ah, yup! If you don’t intend to await you shouldnt async_nolink either, start_child is the way to go.
Also Liked
wojtekmach
You’re right that both
Task.Supervisor.async_nolinkandTask.startare not linked to the current process, that’s often what we want. The difference is the former is linked to the task supervisor. This means when the application goes down, your task will be properly cleaned up too. In the case ofTask.startbecause it’s not linked to anything, it may be left dangling.wolf4earth
This response to a different but similar thread might answer your question:
harmon25
I usually create a named task supervisor in the applications supervision tree. This makes it easier to leverage observer to keep an eye on their execution.