We set the the process to trap exits of its child processes. With this, we encapsulate the supervisor in its own process, so our calling process is not affected by its flags and does not receive its messages. We start a new process linked to our current process. If we spawn a process with this function, it will instantly go into the waiting state and its resource usage (aside from its reserved memory) is pretty much zero. The recursive function will still happily call itself but only after receiving a message. Since the scheduler will ignore waiting processes, it is very cheap to have a process waiting for a message without doing anything. There is more to it than that but we don’t have to care about that at this point. To simplify, a process can be in the running state, if it’s doing something, or in the waiting state, if it’s waiting to receive a new message in its inbox. Processes can have different states internally, which decide how the scheduler will treat them. To understand how we can do this efficiently, we need to understand a simple concept of the BEAM. It’s still not quite ideal, though, if we spawn a process with that function, it will probably kick our CPU usage to 100%įor functions with finite recursions - like a text parser or a sequence generator - it’s ok to run as fast as they can, however, a lot of times we want these things to run forever. In Elixir, a function like the following is not an issue, since tail calls are optimized, so there won’t be an overflow of the call stack. If we want to dumb things down, a (tail) recursive function is a function that calls itself as its last action. Understanding their pros and cons is absolutely vital and defining them properly can make the difference between an extremely clean or a horribly messy codebase. ![]() ⚠️ You should not be like me back then and treat data structures as something I have to learn as well. If you have not yet done your own research, this article is here to slap some code in your face to proof to you how thin of a layer a lot of the shiny stuff in Elixir is and provide some insight on why it works so nicely. I’ve read a few books on Elixir by now and yes, most of them will tell you that this is not the case in Elixir/OTP and things are actually really simple internally but… that’s exactly what someone with lost of complex magic in their modules would say, eh! In short, You’ve probably read about this topic but you probably also didn’t believe it. That’s great but sometimes, this comes at the cost of a framework doing complex magic that you have no hope of deciphering if something ever goes wrong. Sure there’s some basics to sift through like the data types and specific syntax elements but after that, we can finally build a distributed, scalable, performant masterpiece of an application! - Riiigt? Granted, even if you’re a bit more of a sane person, once you get to work with Supervisor, GenServer, Agent and other modules alike, you can’t help but feel that things have been simplified a lot for you. If you’re anything like me, you probably started to learn Elixir and wanted to skip to the shiny stuff right away.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |