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 aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











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: