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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
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
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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: