Skip to content
Browse files

temporary change to mycb where we explicitly set the key of the callb…

…ack lookup and remove the dependency on inspect. it seems looking up the calling function was getting broken somehow in pdb, still tracking down the problem
  • Loading branch information...
1 parent a50f12b commit c7dc825ac435cec00a103da79e5f9a39280c9ff9 Jeremy Kelley committed Mar 2, 2011
Showing with 29 additions and 19 deletions.
  1. +20 −1 tests/test_async_yield.py
  2. +9 −18 tornado_addons/async_yield.py
View
21 tests/test_async_yield.py
@@ -28,9 +28,22 @@ def async_assign(self, newdata, callback):
self.test_ioloop.add_callback(lambda: callback(newdata))
@async_yield
+ def embedded_async(self, callback):
+ xx = yield self.async_assign('me', self.mycb('embedded_async'))
+ 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)
+ results = yield self.async_assign(val, self.mycb('some_async_func'))
+ callback(results)
+
+ @async_yield
+ def call_other_async(self, ioloop, val, callback):
+ cb = self.mycb('call_other_async')
+ self.test_ioloop = ioloop # we have to fake this for tests
+ yield self.embedded_async(cb)
+ results = yield self.async_assign(val, cb)
callback(results)
@@ -53,3 +66,9 @@ def test_async_func_return_more(self):
self.handler.some_async_func(self.io_loop, [1,2,3], self.stop)
retval = self.wait()
self.assertTrue(len(retval) == 3 and retval[1] == 2)
+
+ def test_call_other_async_yield(self):
+ self.handler.call_other_async(self.io_loop, [1,2,3], self.stop)
+ retval = self.wait()
+ self.assertTrue(len(retval) == 3 and retval[1] == 2)
+
View
27 tornado_addons/async_yield.py
@@ -1,8 +1,7 @@
from types import GeneratorType
-import inspect
import tornado.web
-class TwasBrillig(object):
+class WrappedCall(object):
def __init__(self, func, *a, **ka):
self.func = func
self.a = a
@@ -23,7 +22,7 @@ def yield_cb(self, *args, **ka):
If a single value is returned into the callback, that value is returned
as the value of a yield expression.
- i.e.: x = yield http.fetch(uri, self.yield_cb)
+ i.e.: x = yield http.fetch(uri, self.mycb)
The response from the fetch will be returned to x.
@@ -33,9 +32,7 @@ def yield_cb(self, *args, **ka):
retval is None.
It's a little gross but works for a large majority of the cases.
-
"""
-
if args and ka:
self._yield_continue((args, ka))
elif ka and not args:
@@ -59,47 +56,41 @@ def __enter__(self):
return self.yielding
def __exit__(self, exc_type, exc_value, traceback):
- if exc_type: print exc_type
self.a[0].rm_func_callback(self.func.func_name)
def async_yield(f):
f = tornado.web.asynchronous(f)
def yielding_(*a, **ka):
- with TwasBrillig(f, *a, **ka) as f_:
+ with WrappedCall(f, *a, **ka) as f_:
if type(f_) is not GeneratorType:
return f_
else:
- try:
- f_.next() # kickstart it
- except StopIteration:
- print "STOPPED ITERATION"
- pass
+ try: f_.next() # kickstart it
+ except StopIteration: pass
return yielding_
-class AsyncYieldMixin(object):
+class AsyncYieldMixin(tornado.web.RequestHandler):
def prepare(self):
self._yield_callbacks = {}
super(AsyncYieldMixin, self).prepare()
def add_func_callback(self, _id, cb):
self._yield_callbacks[_id] = cb
+ print "adding", _id, cb
def rm_func_callback(self, _id):
del self._yield_callbacks[_id]
- @property
- def mycb(self):
+ def mycb(self, key):
"""
make a callback
"""
- # return inspect.stack()[1][3] # <-- calling functions name
# technically, this just looks up the callback, but eh. whatev
- key = inspect.stack()[1][3]
cb = self._yield_callbacks[key]
- print "\n....... key",key," cb",cb
+ print "\n....... key",key," cb",cb, "\n\n"
return cb

0 comments on commit c7dc825

Please sign in to comment.
Something went wrong with that request. Please try again.