100phlecs

100phlecs

Consequences of choosing `Task.Supervisor` over `Task.async/1`?

Searching around online, it’s always recommended to have processes supervised.

So, instead of Task.async/1 one would use Task.Supervisor.start_child/3 which is what I chose to do.
But I’m wondering what use cases there are for Task.async/1 — the current advisory is to not to use any unsupervised processes as they may turn into non-terminating/zombie processes that would eventually cause problems.

But I guess it’s permissible if you know the Task you’re shooting off will end?

For Task.Supervisor you need to init by adding it your application children.

# application.ex

@impl true
def start(_type, _args) do
  children = [
    # ...
    {Task.Supervisor, name: MyApp.TaskSupervisor}
  ]
  # ...
  Supervisor.start_link(children, opts)
end

Are there unforeseen costs/consequences to doing so, i.e. application load?

To bring this back to the real world, I have an async API call to make to an external service for a side effect and I do not need its response.
I don’t want this API call to block my main process/cause it to hang.

In terms of other modules I could use besides Task, this async API call isn’t within a hotpath of the app, so I don’t see the need for a heavier solution.

Most Liked

lud

lud

Tasks started with Task.async/1 are fine in supervised processes as long as you await them or better, match the response message. Because the task will be linked to the calling process, any error will bubble up.

But in a fire and forget situation, Task.Supervisor.start_child/2 will give you better observability than a mere spawn_link/1. Starting a single supervisor for all your tasks is not heavy at all. So I think your choice was good.

gregvaughn

gregvaughn

That’s one concern, but I don’t think it’s the main one. You can start off a process that runs an infinite loop and the beam handles it well. The larger concern is orderly shutdown behavior. In an orderly shutdown the supervision tree is walked and each supervisor tells its own children to gracefully terminate, within a configurable timeout. Unsupervised processes will never receive notification to shutdown gracefully, so they’ll be brutal killed when the beam shuts down.

dimitarvp

dimitarvp

No, not really. BEAM has been tuned from the get go to have a ton of lightweight processes.

Seems you already know the differences between the two approaches you tried, and you got a good comment after that refining the explanation.

I’ll only add that if you are worried about tasks not terminating then you probably should use Oban.

Where Next?

Popular in Discussions Top

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement