Permalink
Browse files

Adopt adisp from brukva

Thanks to kmike for the hint! This version fixes errors with the stack
limit and improves exception handling!

closes #1
  • Loading branch information...
1 parent a3b543e commit b45b9163dd723d6d8307a099aebcda60945ff371 Daniel Truemper committed Jul 26, 2011
Showing with 11 additions and 3 deletions.
  1. +11 −3 tornadotools/adisp.py
View
@@ -99,19 +99,27 @@ def get_stat():
responses corresponding to given urls.
'''
from functools import partial, wraps
+from tornado.ioloop import IOLoop
class CallbackDispatcher(object):
def __init__(self, generator):
self.g = generator
+ self.io_loop = IOLoop.instance()
try:
self.call(self.g.next())
except StopIteration:
pass
+ def _queue_send_result(self, result, single):
+ self.io_loop.add_callback(partial(self._send_result, result, single))
+
def _send_result(self, results, single):
try:
result = results[0] if single else results
- self.call(self.g.send(result))
+ if isinstance(result, Exception):
+ self.call(self.g.throw(result))
+ else:
+ self.call(self.g.send(result))
except StopIteration:
pass
@@ -122,7 +130,7 @@ def call(self, callers):
self.call_count = len(list(callers))
results = [None] * self.call_count
if self.call_count == 0:
- self._send_result(results, single)
+ self._queue_send_result(results, single)
else:
for count, caller in enumerate(callers):
caller(callback=partial(self.callback, results, count, single))
@@ -132,7 +140,7 @@ def callback(self, results, index, single, arg):
results[index] = arg
if self.call_count > 0:
return
- self._send_result(results, single)
+ self._queue_send_result(results, single)
def process(func):
@wraps(func)

0 comments on commit b45b916

Please sign in to comment.