ollien

ollien

If I have a Task.Supervisor, is there any reason why it would be a bad idea to use functions such as DynamicSupervisor.start_child/2 to start tasks that are managed by this supervisor? Looking at the source code for the Task.Supervisor module, Task.Supervisor.start_link/1 seems to just start a DynamicSupervisor with some settings changed, so it seems there’s no reason this shouldn’t work (and indeed, it does). However, I’m not sure if this is me abusing an implementation detail which may change at any time, or if there is anything necessarily wrong with me doing this.

Of course, it’s not difficult for me to spawn a second DynamicSupervisor myself and avoid the question all together, but I figured I’d ask; it would make some code cleaner if doing this truly was no big deal.

Thanks!

Showing Posts 1 to 10

ollien

ollien OP

Just wanted to give this a bump. Thanks!

tcoopman

tcoopman

What’s the reason you’d want to do this? And why not use the provided Task.Supervisor functions?

ollien

ollien OP

None in particular. I had a task supervisor, and wanted to spawn a more conventional childspec as well (I believe it was spawning an Agent that led me to this question; but really my question is more general). I don’t believe the provided Task.Supervisor functions let you do that.

Like I said in the OP I can always just spawn a second supervisor, but treating the Task.Supervisor as a DynamicSupervisor seemed to work, and I wasn’t sure of the repercussions.

zachallaun

zachallaun

Here’s how I see it: The fact that Task.Supervisor uses a DynamicSupervisor under the hood is an implementation detail that should not be relied on in your code. While it’s conceivable that Task.Supervisor changes its implementation at some point to use something other than DynamicSupervisor, that’s not the reason I wouldn’t do this personally – it’s just kinda poor practice.

joey_the_snake

joey_the_snake

If there’s a a good use case for what you’re doing, you might suggest extending Task.Supervisor to the mailing list. It might get accepted or you might get some info that is helpful.

ollien

ollien OP

Really what we need is one supervisor to handle everyone’s usecases! Jokes aside, I may. I saw a comment from José a while ago about DynamicSupervisor not supporting static children since that’s kind of a niche usecase, and easy enough to spin up two supervisors; I imagine this is another one of those situations. Still, if I bump into it again, I’ll definitely look into that!

trisolaran

trisolaran

I can think of one: it makes your code easier to test. Task.Supervisor implements caller tracking, something that DynamicSupervisor doesn’t do out of the box. Tracking of callers allows Ecto.Sandbox connections and mocks created via Mox to be transparently shared between your test process and its children, even in async mode. Without this mechanism, you would have to use explicit allowances to run async tests.

ollien

ollien OP

Unless I’m misunderstanding you, I think you read the question backwards. Wouldn’t that be a reason to use DynamicSupervisor functions on a Task.Supervisor (putting aside the above about it being an implementation detail)?

trisolaran

trisolaran

You’re asking: “if I use DynamicSupervisor.start_child/2 and co. instead of TaskSupervisor.start_child/2 and co. on a Task.Supervisor, are there drawbacks?”

Did I misunderstand this?

If not, I’m offering you an example of a drawback, which is a reason for sticking to Task.Supervisor functions.

the functions in Task.Supervisor implement caller tracking, so if your module Foo does Task.Supervisor.start_child(Task.Supervisor, my_task), you’ll get caller tracking. See the docs and also here. What it’s good for: when testing Foo, Ecto.Sandbox connections and Mox’s mocks created in your test process are transparently made available to my_task (and btw, this mechanism is why you can use your sandboxed connections in your LiveViews during testing, if you ever wondered).

On the other hand, if your module Foo does DynamicSupervisor.start_child(Task.Supervisor, my_task), you won’t get any caller tracking. If you want your sandboxed conenctions and mocks to be available to my_task, you’ll have to use allowances. This means adding extra code to Foo that is only there to enable testing, so generally not a good thing.

So: +1 for sticking to Task.Supervisor functions, and no reason to use DynamicSupervisor functions.

Let me know if this is still unclear.

ollien

ollien OP

No, but I realize now I could have phrased the original better :slight_smile:

When I said “start tasks” here, I should have said start processes. In other words, not specifically a Task (I used “task” generally here; bad idea!). I was actually thinking of launching non-Task processes, such as Agents.

Regardless, I see what you’re getting at now that I see the confusion. For the case of starting a literal Task, that makes sense. Thank you!

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
kpanic
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
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 Top

GenericJam
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
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews