stefanchrobot
I need to:
- do some asynchronous work concurrently
- kill the work when the parent
GenServerexits - be able to specify the timeout
- do some bookkeeping when the work succeeds
- do some other bookkeeping when the work fails
Is the Task.Supervisor the right tool for the job?
I’m thinking of writing a GenServer that would use Task.Supervisor.async_nolink to start Tasks, specifying the :shutdown option and handling the {ref, result} and :DOWN messages.
But how do I make sure that the tasks are immediately terminated when the GenServer is shut down? I guess Task.Supervisor.async is what I should use. But how will I get the results/crashes? Am I going to receive the same messages as with async_nolink?
Trending in Questions
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
joaquinalcerro
@sasajuric is working in this project that might help your use case:
GitHub - sasa1977/parent: Custom parenting of processes in Elixir · GitHub
Check it out.
josevalim
By parts:
Use a
Task.SupervisorUse
Task.Supervisor.asyncbut notice you can’t use await, you will receive the done or the down message in yourhandle_infoWhen you call
Task.Supervisor.async, also callProcess.send_after(self, {:kill, task}, timeout). You will receive that message Inhandle_info, kill the task if timeout has passedMatch the
{ref, result}message inhandle_infoand cancel the timeout started aboveMatch the
DOWNmessage inhandle_infoand cancel the timeout started above. Also make sure you callProcess.flag(:trap_exit, true)so your genserver doesn’t die when the task crashesstefanchrobot
Thanks for the response, that’s really helpful!
Things really clicked when I realized that tasks run under
Task.Supervisorare always linked to that supervisor.Task.Supervisor.asyncandTask.Supervisor.async_nolinkare used to decide whether to link (or not) to the caller of those functions, which should be aGenServerthat should probably do something about the messages from the tasks.Then I also realized that if I want to do any extra bookkeeping after the tasks complete, it has to be done in some
GenServer, not in a supervisor.Once I’m done with my stuff, I’d like to work on a PR to improve the docs for the
Task.Supervisor.joaquinalcerro
I was following this thread due to how common this use case is as expressed by @stefanchrobot and and the solution @josevalim gave seemed interesting and simple.
So I went and code it myself as I understood the use case and Task documentation (https://hexdocs.pm/elixir/1.5/Task.html#content).
It took me a while to grasp but this is the code. Please correct me if there is anything I misunderstood:
Crete the project
The supervision tree
The basic task
The GenServer
stefanchrobot
@joaquinalcerro
Stask.Serverunder the same supervisor in theStask.Application.task.pid→timer_refin the GenServer.Process.exit(task.pid, :kill), since I wanted a hard timeout. It makes thathandle_infomuch simpler - you don’t have to repeat the logic from handling the:DOWNmessage.@josevalim I’m not sure what’s the criteria for picking
Task.Supervisor.async_nolinkvsTask.Supervisor.async. Can you shed some light on that?In the first case, I don’t need to trap exits and only have to handle the
:DOWNmessage. In the latter, I need to handle both:DOWNand:EXIT. My GenServer is linking to some other processes (RabbitMQ connection), so doingasync_nolinkseems more convenient (if RabbitMQ connection goes down, I need my GenServer to go down as well).joaquinalcerro
So based on your first two comments the use case was to have a single GenServer coordinating the execution of async tasks?
I think that @josevalim suggested the
Task.Supervisor.asyncbecause it links the task to the caller and you will be able to trap the exit signal to do the book keeping. TheTask.Supervisor.async_nolinkwill not link the task to the caller so you won’t be able to know when the task fails.Lets wait for his comments.
Thanks
stefanchrobot
Yes, that’s exactly my case.
Not really, see the docs:
joaquinalcerro
Oh… I missed the “Compatibility with OTP behaviours” section. I read the async_nolink/3 documentation and missed the async_nolink/4.
So async_nolink will not link the task to the caller. Without the link, the caller will not receive the :EXIT signal and therefor it will not crash if the task crashes but the caller will always receive the :DOWN message with the result once the task finishes… cool.
Thanks
csangonzo
I might be a few years late
but here’s what I did today: