def relax(_func_) : """ A decorator for simplifying async couchdb access from Tornado. Methods using this decorator will be able to make asynchronous calls to the database without needing to provide explicit callbacks (and restoring traditional exception handling in the process). It decides between applying tornado's ``@asynchronous`` and ``@gen.engine`` decorators as appropriate for a given method. Its primary function is to intercept yield statements in your request handler and to evaluate them asynchronously (with an automatic callback that it inserts). When the database request completes, the callback will resume your function at the yield point and pass the received data off with a simple assignment. """ try: from tornado import web, gen def _r_e_l_a_x_(*args, **kwargs): return gen.engine(_func_)(*args, **kwargs) if _func_.__name__ in 'head|get|post|delete|put|options'.split('|'): return web.asynchronous(_r_e_l_a_x_) else: return _r_e_l_a_x_ except ImportError: return _func_
def __new__(cls, clsname, bases, attrs): allow_method = ['get', 'put', 'delete', 'post', 'options', 'patch'] for method in attrs: if method.lower() in allow_method: attrs[method] = coroutine(asynchronous(attrs[method])) return super(GenAsyncMetaclass, cls).__new__(cls, clsname, bases, attrs)
def decorator(func): func = asynchronous(func) def wrapper(*args, **kwargs): start_time = time() request = args[0] callback = lambda: finish_request(request, start_time) request.ioloop.add_timeout(time() + seconds, callback) return func(*args, **kwargs) return wrapper
def decorator(func): frm = inspect.stack()[1] class_name = frm[3] module_name = frm[0].f_back.f_globals["__name__"] full_class_name = module_name + '.' + class_name real_pattern = '^' + pattern + '$' add_handler(method, real_pattern, full_class_name, func) return asynchronous(func)
def async_engine(func): return web.asynchronous(gen.coroutine(func))