So am asking this question because I cannot seem to find a simple worded answer when running a search through the Python Library, Google, and Stack Exchange.
The best I could find so far is from wptserve:
"Handlers are functions that have the general signature:
handler(request, response)
It is expected that the handler will use information from the request (e.g. the path) either to populate the response object with the data to send, or to directly write to the output stream via the ResponseWriter instance associated with the request."
link: https://wptserve.readthedocs.io/en/latest/handlers.html
So basically, from what I gather, handlers respond to a user-request with a response, via the use of data, with the type of data-dependent upon the type of request.
I am sure it's a bit more complicated than that, but again, what's really needed is a very simply worded, conceptual description of what handlers are, without the fancy terminology.
Introduction to Python Event Handler. An Event Handler is a class, part of events, which simply is responsible for managing all callbacks which are to be executed when invoked. An Event is simply an action, like clicking a button, which then leads to another event, which the developer already assigns. The core of Python Event Handler is ...
A handler is a routine/function/method which is specialized in a certain type of data or focused on certain special tasks.
If respect_handler_level is True, a handler’s level is respected (compared with the level for the message) when deciding whether to pass messages to that handler; otherwise, the behaviour is as in previous Python versions - to always pass each message to each handler. Changed in version 3.5: The respect_handler_level argument was added.
For me, it usually means some code that is called from an inner core and is supposed to do some things and return. That 'inner' part can have several 'handlers' available, and chooses which one to call.
In general handlers are functions that 'handle' certain events that they are registered for.
The documentation that you reference is about a Web Server. A web server receives requests from e.g. Browsers for URLs - and returns an answer depending on what was requested. The specific handler they are talking about takes in two arguments:
request
that is coming from the browser and contains information like the URL, maybe cookies, an IP etc.response
object. The handler shall take the information from the request
to fill that response
object accordingly. It might use other information like databases or files as well.For example: A web browser requests http://me.com/index.html
. The handler is invoked, looks for a file named index.html
in the root directory. If the file is found, it is read, appended to the response
object and the handler is done. The wgetsrv
framework will do the rest of the work and send the response back to the client who will now see a website.
There are many other types of handlers, for example for user input. A registered keyboard input handler is invoked on every keystroke with the key typed. This allows the programmer to react and display text or invoke shortcuts.
Handlers are often used to replace polling
. Polling
is a technique where the program is checking the state of something frequently to react when the state has changed. Staying with the keyboard example. Imagine you are steering a player in a game through a 2D world with your arrow keys. How does the program know which direction you want to go? It needs to check which (if any) of the arrow keys are pressed. For that, it could get the state of the keyboard, check if e.g. the right arrow key is currently pressed and then move the player a bit to the right if that is the case. But the program will only know about this change once it has checked the keyboard. So to be responsive for the player it has to check the state of the keyboard quite often (a couple of times per second) which wastes CPU cycles as most of the time the state has indeed not changed.
Now turning the model around, the game could ask the keyboard: Hey, when a key is pressed, call this function and tell it which keys are pressed
. The function can then accordingly update the players position - but is only called when the state of the keyboard has changed. Polling is like calling the pizza guy every five minutes: "Is my order ready for pickup?" as opposed to calling him once and telling him "Call me back when my order is ready." In the latter case you 'registered a handler for pizza-ready events' and saved both of you a lot of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With