-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtest_yielddefer25.py
118 lines (99 loc) · 3.13 KB
/
test_yielddefer25.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import generators
app_name = "test"
from BTL.defer import Deferred, Failure
from BTL.yielddefer25 import launch_coroutine, wrap_task
from BTL.reactor_magic import reactor
import time
import thread
import threading
def wait(df, v):
if v == 'bar':
df.errback(ValueError("The value was bar!"))
return
time.sleep(1)
#print "done:", v
df.callback("post " + v)
rawserver = None
import Queue
queue = Queue.Queue()
def run():
global queue
while True:
df = queue.get()
if df is None:
break
if df.error:
try:
fdsfs = tasty
except:
reactor.callFromThread(df.errback, Failure())
else:
reactor.callFromThread(df.callback, 'post')
mt = threading.Thread(target=run).start()
def task(name, error=False):
df = Deferred()
df.error = error
queue.put(df)
return df
foo = lambda : task('foo')
bar = lambda : task('bar')
baz = lambda : task('baz', error=True)
class Worker(object):
def __init__(self):
self.holder = None
self.ident = thread.get_ident()
def do_some_things(self):
for t in [foo, bar, baz]:
try:
r = yield t()
if t == baz: assert False
#print "result:", r
except Exception, e:
if t != baz: assert False
#print "An (expected) error occurred:", e
pass
if __name__ == '__main__':
w = Worker()
w.total = 0
def set_event(x):
w.total -= 1
assert w.total >= 0
if w.total == 0:
reactor.stop()
def set_event_error(x):
print "A critical error occured:", str(x[0]), ":", str(x[1])
reactor.stop()
def run_task_and_exit():
#for i in xrange(1000):
for i in xrange(100040):
w.total += 1
df = launch_coroutine(wrap_task(reactor.callLater),
w.do_some_things)
df.addCallback(set_event)
df.addErrback(set_event_error)
#reactor.callLater(0, run_task_and_exit)
w.total = 0
def run(r=None):
if w.total > 1000:
reactor.stop()
return
w.total += 1
df = launch_coroutine(wrap_task(reactor.callLater),
w.do_some_things)
df.addCallback(run)
df.addErrback(set_event_error)
reactor.callLater(0, run)
reactor.run()
queue.put(None)