Загрузка страницы

Learn Elixir: OTP, GenServers, Agents, & Tasks

Level 5.1.3: OTP, GenServers, Agents, & Tasks; learn the fundamentals of how we use OTP inside of Elixir.

For more information on Learn Elixir please visit https://learn-elixir.dev/

Follow us on Facebook or LinkedIn for the latest in Elixir news, articles, or updates:
https://www.facebook.com/LearnElixir
https://www.linkedin.com/company/learn-elixir/
Transcript:
Hello and welcome to learn elixir. Week five module one. In this video we're going to be talking about the fundamentals of how we use OTP inside of elixir and that's gen servers, agents and tasks. These three are all process of abstractions that allow us to control and manage a process by using an abstraction that makes it easier to actually work with that process. Each three of these have very defined roles, however, a few of them do very similar things so it can actually be a bit confusing in which case we use what we'll start out with gen servers which stand for general server. These can be used to receive and send messages and have a callback pattern that gets declared in order to handle and receive these messages. We can also store a state within these gen servers, which is how we get state inside of elixir. If you've noticed. So far we haven't really held any state inside of elixir because we have our database that holds the States for us and each of our calls are simple functions, so we don't have a way to actually store data into a specific module. We can store state inside of elixir by using a process and so a process can have its own sense of state to do this. We normally use gen servers or agents since they have the most well rounded features in order to manage. Our state agents are similar to Jen servers, however, they can't receive custom messages. You can send messages from any process you want since send is available globally but you can't actually receive a message from within. An agent. Agents are OTP is process abstraction around using state simply so if we have some state that doesn't have any behaviors involved in it, we would use an agent over a gen server. As soon as we start handling some logic with that state itself, we probably want them to start moving over to a gen server and allow that gen server to code the logic as well as fetch the data from the state. Agents however, are extremely useful because they're a simple abstraction and accessing an agent can actually be quicker than even accessing ETS in some cases. ETS is an Earl Lang term store that's actually created by Erling that allows us to store values and access them concurrently. However, because agents are so simple, it can actually be faster to use the agents instead. In some situations, our final objection is known as a task. These can't receive custom messages either similar to an agent, so if you're trying to receive messages, you're better off with a gen server. You also can't give tasks and name so you cannot give a task name because it's not going to be able to receive any messages. Tasks are often done for a simple onetime use or as a way of setting up polling so that we can use a single process and make our coats sequential. You can start a task on any node and you can run a task on the current node. This give tasks the power of being able to be started on any single node and you can dynamically scale your across your system. It's a very simple process of extraction tasks and it's only responsible for managing one process at a time. A single task is akin to a single process. With all this in mind. Let's take a look at some example implementations of using an agent gen server and task. I've gone ahead and set up a simple gen server and our gen server, we're allowing messages to be added to a queue and then we have another call to be able to fetch all those messages and view them. We started a gen server by adding this call to use the gen server. This defines the child spec function for us and allows it to set up what a GenServers would look like so we can add it to our application tree quite easily. We then define a start link function as is standard in elixir with some options. How you define the start link function is really up to you, but I found this way of defining it to be quite helpful because what you can do is define the options as a defaulted empty argument and then have functions that work based off the assumption that there could be possible values such as put new where we only put the name if a name doesn't already exist and we can then use our options to pass into the GenServer start link options. This is advantageous because we might want a default name here, but when we're creating tests, having a default name might actually be a hindrance or perhaps we want to name the server something else or even run multiple copies of this GenServer. Doing so with a default name allows us to actually run this just naturally but also allows us to configure it.....

Видео Learn Elixir: OTP, GenServers, Agents, & Tasks канала Learn Elixir
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
24 февраля 2021 г. 6:22:07
00:13:05
Яндекс.Метрика