Elixir GenServer callbacks and concurrency

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

You know exactly where to stop them so I want to see more.

Good job!

My only critique comes from production rather than content: The audio is a little quiet.

👍︎︎ 1 👤︎︎ u/old_to_me_downvoter 📅︎︎ Dec 05 2016 🗫︎ replies

I am really liking these videos so far, thank you.

👍︎︎ 1 👤︎︎ u/SpaceSword 📅︎︎ Dec 07 2016 🗫︎ replies
Captions
Jen server callbacks help abstract message receiving functionality of elixir processes these abstractions provide a way to receive messages from other Jen server processes as well as custom messages or messages coming from system this presentation is going to go over the syntax and structure of some callback methods provide a few use cases and examples and talk about how Jen server could help us take advantage of elixirs ability to execute code concurrently if you are new to Jen server concepts like initialization and callbacks please check out a previous video in this series that goes over Jen server basics and provides a few introductory examples Jen server has three callbacks that help abstract process communication handle call handle cast and handle info their syntax is very similar with a few important differences when they are called they all get access to the current state of the Jen server process and they all get to reset the state in their response handle call also receives a from argument when it gets called and it replies with a three element topple with the second element being the return value handle cast and handle info respond with a two element tuple no reply in the new state of the process please note that because they have no return value the first element of the response tuple is no reply unlike the reply in handle call let's start a Jen server process and go over how these methods are used handle call gets triggered by Jen server call method typically a method that runs Jen server call will expect some kind of a return value in this case what you're going to see is this output with a response variable being an empty list we assigned during initialization handle call a synchronous or blocking meaning it will not execute any code below Jen server call until it receives a reply this method can be used for both setting and retrieving state information in the specific Jen server process handle cast gets triggered by Jen server cast this is a method you want to use when you don't need to receive an immediate reply and want to take advantage of elixirs ability to execute code and Oh cast is a slightly different syntax from handle call mainly there are less elements to worry about it does not have a from argument and it does not have a reply you still have to respond with a couple but it will have two elements instead of three handle cast is asynchronous or non-blocking meaning it will not wait for the method to finish before executing the code below it inside it you'll have access to gen server process state and you can set it but don't expect this method to return data when you run it the only thing Jenson forecast returns is okay handle info has syntax that is pretty much identical to handle cast it's also asynchronous the difference is there isn't an explicit gen server trigger method associated with it there is no gen server dot info handle info is used for receiving messages from everyone except the gen server it can receive messages from any elixir process to test it you could use a looks or process module process send method takes three arguments the process ID the message tuple and options internally a Hindle info works exactly the same way handle cast does you have access to the process state and you can reset it and just like handle cast it always responds with ok handle info could be used for a number of different things could be used for caching shutdown messages for gen server processes and you can use it for logging or notifications but as you saw any process could send handle info a message when thinking about concurrency and synchronous versus asynchronous execution I like to use a package delivery analogy let's say there is a delivery company called L hex they provide two types of deliveries why'd you have to sign for and once the delivery person can just leave by the door the deliveries that have to get signatures are synchronous the delivery person has to wait for you to sign for the package before making any other deliveries that have to wait for the recipient to respond for every package no matter how fast they can get it there on the other hand packages they leave by the door are asynchronous they leave them maybe ring the doorbell and leave they don't care when you're going to pick it up and at the same time they did their job asynchronous package delivery is much more efficient for synchronous delivery you are required to wait for response for each package and typically you have no control over the response time for asynchronous delivery you are not blogged for each package and can therefore function much more efficiently you don't have control over when the package is going to be received and in terms of a delivery business you don't really care unless something goes wrong let's continue with this analogy and see it in action in this example we're going to have two modules a gen server package receiver and some regular module that's going to be calling it it isn't necessary to have another module for the caller but it helps to keep their concerns separate delivery module is going to define a list start a package receiver gen server process enumerate over each package and call leave at the door method of the package receiver leave at the door method is going to call gen server chest method even though inside of this method each package is going to take one second to be received this will not block the iteration in fact you're going to see all done with deliveries before we see the first package being received let's see it in action when we call this method we're going to see all done with deliveries printed way before we're going to see I received a package after second passes you're going to see I received a package printed one second at a time as you can see we got all done with deliveries almost right away and each delivered package took a second to be received so this is an example of a synchronous elixir code in action while this was a good start we still took a total of four seconds to receive all the packages it's important to understand that while we are calling and Jen server processes synchronously it still has to process one message at a time each elixir process is a mailbox that receives all the messages addressed to them what we've done here is send all four messages to this one process but this process still had to deal with them one at a time each one taken a second one way to address this is to start one process for every message in which case it will only take about a second to process all for now this is far from best practices but one way we can demonstrate this here is to start a Jen server process inside the enumerator this will start a process for every package when you run this method you're going to see all done with deliveries right away and all the package received messages a second later that will all be printed at the same time as I mentioned before this is far from best practices we don't want to start an unknown number of jian server processes with no way of knowing what's going on with them how do we know if leave by the door method succeeded and what should happen if it doesn't elixir has a specific way of keeping an eye on Jen server processes with a module called a supervisor supervisors can handle stopping restarting and generally monitoring Jen server processes in a robust and flexible way but more on that at another time the goal for this video was to cover the Gen Server essentials for you to continue learning and perhaps starting to test out gen server in your own projects I hope this provided you with a good starting point please keep rivas than in the gen server documentation and continue to expand your elixir and gen server knowledge thank you for watching
Info
Channel: omgneering
Views: 7,938
Rating: 5 out of 5
Keywords: elixir, gen_server, genserver, concurrency
Id: 4yogCxBoO-A
Channel Id: undefined
Length: 8min 21sec (501 seconds)
Published: Mon Dec 05 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.