100phlecs

100phlecs

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.

Showing Posts 1 to 3

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.

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.

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews