in the embedding examples thus farc has been running and calling python code from standard main program flow of control things are not always so simplethoughin some casesprograms are modeled on an event-driven architecture in which code is executed only in response to some sort of event the event might be an end user clicking button in guithe operating system delivering signalor simply software running an action associated with an entry in table in any event (pun accidental)program code in such an architecture is typically structured as callback handlers--units of code invoked by event-processing dispatch logic it' easy to use embedded python code to implement callback handlers in such systemin factthe event-processing layer can simply use the embedded-call api tools we saw earlier in this to run python handlers the only new trick in this model is how to make the layer know what code should be run for each event handlers must somehow be registered to to associate them with future events in generalthere is wide variety of ways to achieve this code/event association for instancec programs canfetch and call functions by event name from one or more module files fetch and run code strings associated with event names in database extract and run code associated with event tags in html or xml run python code that calls back to to tell it what should be run and so on reallyany place you can associate objects or strings with identifiers is potential callback registration mechanism some of these techniques have advantages all their own for instancecallbacks fetched from module files support dynamic reloading (imp reload works on modules but does not update objects held directlyand none of the first three schemes require users to code special python programs that do nothing but register handlers to be run later it is perhaps more commonthoughto register callback handlers with the last approach--letting python code register handlers with by calling back to through extension interfaces although this scheme is not without trade-offsit can provide natural and direct model in scenarios where callbacks are associated with large number of objects for instanceconsider gui constructed by building tree of widget objects in python scripts if each widget object in the tree can have an associated event handlerit may be easier to register handlers by simply calling methods of widgets in the tree associating handlers with widget objects in separate structure such as module file or an xml file requires extra cross-reference work to keep the handlers in sync with the tree in factif you're looking for more realistic example of python callback handlersconsider the tkinter gui system we've used extensively in this book tkinter uses both python/ integration