I am trying to learn OTP by looking at good examples of projects using OTP. I studied the code for mix. But looks like mix is not using much of OTP.
To me, mix
has lot of possible usecases for OTP. Examples like, compiling multiple files concurrently, downloading deps concurrently etc.
I was wondering why mix is not using OTP? Or is OTP overkill for a CLI application? Does it only make sense for long running applications?
I have also noticed that mix doesn’t run things concurrently. Is there a reason for this too?
5 Likes
That’s an interesting question. I guess the easy answer would be “because it doesn’t need”.
Your other points about concurrent file compilation and so on make me think that it wasn’t done because of race conditions and locks maybe? I’m not versed in compilers. Also may be something that can change from OS to OS and maybe it’s a design decision in order to support Unix and Windows.
3 Likes
Perhaps I missed something but i was sure elixir compiler runs (or can) concurrently.
And mix generates an OTP application for you and knows how to work with it.
2 Likes
I think @josevalim could provide best answer here as it seems it is pretty much his design on his own judging from the commit history to mix directory
https://github.com/elixir-lang/elixir/commits/master/lib/mix?page=49
The thing is, it does use OTP stuff. It starts supervision tree, TasksServer is an Agent (not technically a pure OTP thing though). It can’t do parallel stuff because I would suspect it increased complexity by a factor of 10.
That said, the tasks themselves can perform things in parallel, look at compilers/elixir.ex.
The second case when it performs operations in parallel is when you execute task from umbrella root, it then recursively runs the same task within apps.
So, I’d wait for proper answer from Jose, but I’d say:
- It uses OTP as much as it needs to, no less no more
- It does not try to be concurrent too hard because you generally want your tasks structure to be sequential, like a recipe, and this makes dependencies simple
2 Likes
Mix is using OTP just fine. It defines an OTP application, its supervision tree and a couple of processes to be part of tree.
When using Mix, it will also download files in parallel, run tests in parallel and compile code in parallel. However, those are done by Hex, ExUnit and the Elixir compiler respectively, that’s why you won’t find them in the Mix codebase itself.
12 Likes