-
Notifications
You must be signed in to change notification settings - Fork 288
/
defer-11.py
63 lines (46 loc) · 1.25 KB
/
defer-11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from twisted.internet.defer import Deferred
print("""\
This example illustrates how deferreds can
be fired before they are returned. First we
make a new deferred, fire it, then add some
callbacks.
""")
# three simple callbacks
def callback_1(res):
print('callback_1 got', res)
return 1
def callback_2(res):
print('callback_2 got', res)
return 2
def callback_3(res):
print('callback_3 got', res)
return 3
# We create a deferred and fire it immediately:
d = Deferred()
print('Firing empty deferred.')
d.callback(0)
# Now we add the callbacks to the deferred.
# Notice how each callback fires immediately.
print('Adding first callback.')
d.addCallback(callback_1)
print('Adding second callback.')
d.addCallback(callback_2)
print('Adding third callback.')
d.addCallback(callback_3)
print("""
Because the deferred was already fired, it
invoked each callback as soon as it was added.
Now we create a deferred and fire it, but
pause it first with the pause() method.
""")
# We do the same thing, but pause the deferred:
d = Deferred()
print('Pausing, then firing deferred.')
d.pause()
d.callback(0)
print('Adding callbacks.')
d.addCallback(callback_1)
d.addCallback(callback_2)
d.addCallback(callback_3)
print('Unpausing the deferred.')
d.unpause()