Skip to content

Commit

Permalink
we now just maintain the previous callback and let the yield_cb unrol…
Browse files Browse the repository at this point in the history
…l as each async function returns back up to it's caller
  • Loading branch information
Jeremy Kelley committed Mar 7, 2011
1 parent 4182ef1 commit 1a761d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
6 changes: 3 additions & 3 deletions tests/test_async_yield.py
Expand Up @@ -29,18 +29,18 @@ def async_assign(self, newdata, callback):

@async_yield
def embedded_async(self, callback):
xx = yield self.async_assign('me', self.mycb('embedded_async'))
xx = yield self.async_assign('me', self.yield_cb)
callback(xx)

@async_yield
def some_async_func(self, ioloop, val, callback):
self.test_ioloop = ioloop # we have to fake this for tests
results = yield self.async_assign(val, self.mycb('some_async_func'))
results = yield self.async_assign(val, self.yield_cb)
callback(results)

@async_yield
def call_other_async(self, ioloop, val, callback):
cb = self.mycb('call_other_async')
cb = self.yield_cb
self.test_ioloop = ioloop # we have to fake this for tests
yield self.embedded_async(cb)
results = yield self.async_assign(val, cb)
Expand Down
23 changes: 7 additions & 16 deletions tornado_addons/async_yield.py
Expand Up @@ -48,19 +48,18 @@ def yield_cb(self, *args, **ka):

def __enter__(self):
# munge this instance's yield_cb to map to THIS instance of a context
obj = self.a[0]
self.old_yield_cb = obj.yield_cb
obj.yield_cb = self.yield_cb
self.yielding = self.func(*self.a, **self.ka)
if type(self.yielding) is GeneratorType:
# the first member of self.a is going to be the instance the
# function belongs to. attach our yield_cb to that
self.a[0].add_func_callback(self.func.func_name, self.yield_cb)
return self.yielding

def __exit__(self, exc_type, exc_value, traceback):
self.a[0].rm_func_callback(self.func.func_name)
obj = self.a[0]
obj.yield_cb = self.old_yield_cb


def async_yield(f):
# f = tornado.web.asynchronous(f)
def yielding_(*a, **ka):
with WrappedCall(f, *a, **ka) as f_:
if type(f_) is not GeneratorType:
Expand All @@ -73,6 +72,8 @@ def yielding_(*a, **ka):

class AsyncYieldMixin(tornado.web.RequestHandler):

yield_cb = lambda *a, **ka: None

def prepare(self):
self._yield_callbacks = {}
super(AsyncYieldMixin, self).prepare()
Expand All @@ -84,14 +85,4 @@ def add_func_callback(self, _id, cb):
def rm_func_callback(self, _id):
del self._yield_callbacks[_id]

def mycb(self, key):
"""
make a callback
"""
# technically, this just looks up the callback, but eh. whatev
cb = self._yield_callbacks[key]
print "\n....... key",key," cb",cb, "\n\n"

return cb


0 comments on commit 1a761d6

Please sign in to comment.