josefrichter

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!

Showing Posts 1 to 6

wojtekmach

wojtekmach

Hex Core Team

You’re right that both Task.Supervisor.async_nolink and Task.start are 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 of Task.start because it’s not linked to anything, it may be left dangling.

wolf4earth

wolf4earth

This response to a different but similar thread might answer your question:

harmon25

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.

josefrichter

josefrichter OP

Thank you all for additional explanation and links. Makes perfect sense to have a supervised Task rather than unhinged one flying around!

josefrichter

josefrichter OP

Hmm, seems like I actually have to run Task.Supervisor.start_child in my case. This approach is mentioned here Task — Elixir v1.20.2

When I try Task.Supervisor.async_nolink, I actually get error that I have undefined handle_info function. The documentation here Task.Supervisor — Elixir v1.20.2 states the following:

If you create a task using async_nolink inside an OTP behaviour like GenServer , you should match on the message coming from the task inside your GenServer.handle_info/2 callback.

Keep in mind that, regardless of how the task created with async_nolink terminates, the caller’s process will always receive a :DOWN message with the same ref value that is held by the task struct. If the task terminates normally, the reason in the :DOWN message will be :normal .

So is the start_child the real fire & forget one? @wojtekmach @wolf4earth @harmon25

wojtekmach

wojtekmach

Hex Core Team

Ah, yup! If you don’t intend to await you shouldnt async_nolink either, start_child is the way to go.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews