stefanchrobot
Supervising async tasks
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 in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Marked As Solved
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 crashesAlso Liked
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
csangonzo
I might be a few years late
but here’s what I did today:
stefanchrobot
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.