An event loop is a loop which handles / deals with events.
An event is something that occurs in the system where some code parts might be interested in.
At the beginning, all components register for events, and after that, an init event is fired:
I am just providing raw code here:
listeners = [component1, component2, component3]
eventqueue.add(InitEvent())
while True:
event = eventqueue.pop()
for listener in listeners:
listener.handle_event(event)
How an eventqueue
is implemented and what the Event()
class hierarchy looks like is left as an exercise for the reader. Take care about using threading.(R)Lock
s etc. for the .pop()
method.
Additionally, you could have separate listener lists for each event type. An event could thus be "fired" by just calling it (or its .fire()
method) and have a mechanism to identify all its own and parent's listeners in order to inform them about the event.
In any case, the listeners then can decide on their own what to do with and according to the event.