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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 9 to 1- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
csangonzo
I might be a few years late
but here’s what I did today:
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
stefanchrobot
Yes, that’s exactly my case.
Not really, see the docs:
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
@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
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
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.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 crashesjoaquinalcerro
@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.