Don't Call Us, We'll Call You:: Callback Patterns and Idioms in Python
Don't Call Us, We'll Call You:: Callback Patterns and Idioms in Python
Ha
("Detach")
Ri
("Transcend")
Sunday, March 9, 2008
2
2
"Callback" implementation
give "somebody" a callable
the "somebody" may store it "somewhere"
a container, an attribute, whatever
or even just keep it as a local variable
and calls it "when appropriate"
when it needs some specific functionality
(i.e., for customization)
or, when appropriate events occur (state
changes, user actions, network or other
I/O, timeouts, system events, ...)
4
Sunday, March 9, 2008
Customization
5
Sunday, March 9, 2008
6
Sunday, March 9, 2008
OO customizing: the TM DP
"Template Method" Design Pattern: perform
the callbacks by "self delegation":
class TMparent(object):
...self.somehook()...
Customizing scheduling
sched needs TWO callback functionalities:
what time is it right now?
wait (sleep) until time T
the OO way (more structured):
import time
s=sched(time)
s=sched(time.time, time.sleep)
Events
9
Sunday, March 9, 2008
10
Sunday, March 9, 2008
10
The Observer DP
a "target object" lets you add "observers"
could be simple callables, or objects
when the target's state changes, it calls
back to "let the observers know"
design choices: "general" observers
(callbacks on ANY state change), "specific"
observers (callbacks on SPECIFIC state
changes; level of specificity may vary),
"grouped" observers (objects with >1
methods for kinds of state-change), ...
11
Sunday, March 9, 2008
11
Callback issues
what arguments are to be used on the call?
no arguments: simplest, a bit "rough"
in Observer: pass as argument the target
object whose state just changed
lets 1 callable observe several targets
or: a "description" of the state changes
saves "round-trips" to obtain them
other: identifier or description of event
but -- what about other arguments (related
to the callable, not to the target/event)...?
12
Sunday, March 9, 2008
12
13
13
Callback "dispatching"
what if more than one callback is set for a
single event (or, Observable target)?
remember and call the latest one only
simplest, roughest
remember and call them all
LIFO? FIFO? or...?
how do you _remove_ a callback?
can one callback "preempt" others?
can events (or state changes) be "grouped"?
use object w/methods instead of callable
14
Sunday, March 9, 2008
14
http://www.python.org/pycon/papers/deferex/
15
Scheduled callbacks
standard library module sched
s = sched.Sched(timefunc, delayfunc)
e.g, Sched(time.time, time.sleep)
evt = s.enter(delay, priority, callable, arg)
or s.enterabs(time, priority, callable, arg)
may s.cancel(evt) later
s.run() runs events until queue is empty (or
an exception is raised in callable or
delayfunc: it propagates but leaves s in
stable state, s.run can be called again later)
16
Sunday, March 9, 2008
16
System-events callbacks
for system-events:
atexit.register(callable, *a, **k)
oldhandler = signal.signal(signum, callable)
sys.displayhook, sys.excepthook,
sys.settrace(callable)
readline.set_startup_hook,
set_pre_input_hook, set_completer
17
Sunday, March 9, 2008
17
Q&A
http://www.aleax.it/pyc08_cback.pdf
!
18
18