Handling the server life cycle
Throughout this book, we have spent a lot of time talking about the life cycle of an incoming HyperText Transfer Protocol (HTTP) request. In that time, we have seen how we can run code at different points in that cycle. Well, the life cycle of the application server as a whole is no different.
Whereas we had middleware and signals, the server life cycle has what are called "listeners". In fact, listeners are in effect (with one small exception) signals themselves. Before we look at how to use them, we will take a look at which listeners are available.
Server listeners
The basic premise of a listener is that you are attaching some function to an event in the server's life cycle. As the server progresses through the startup and shutdown process, Sanic will trigger these events and therefore allow you to easily plug in your own functionality. Sanic triggers events at both the startup and shutdown phases. For any other event during...