Function Reference

greenado.concurrent

exception greenado.concurrent.TimeoutError[source]

Bases: Exception

Exception raised by gyield in timeout.

greenado.concurrent.gcall(f, *args, **kwargs)[source]

Calls a function, makes it asynchronous, and returns the result of the function as a tornado.concurrent.Future. The wrapped function may use gyield() to pseudo-synchronously wait for a future to resolve.

This is the same code that @greenado.groutine uses to wrap functions.

Parameters:
  • f – Function to call
  • args – Function arguments
  • kwargs – Function keyword arguments
Returns:

tornado.concurrent.Future

Warning

You should not discard the returned Future or exceptions may be silently discarded, similar to a tornado coroutine. See @gen.coroutine for details.

greenado.concurrent.generator(f)[source]

A decorator that allows you to use the ‘yield’ keyword in a function, without requiring callers to also yield this function.

The yield keyword can be used inside a decorated function on any function call that returns a future object, such as functions decorated by @gen.coroutine, and most of the tornado API as of tornado 4.0.

Similar to @gen.coroutine, in versions of Python before 3.3 you must raise tornado.gen.Return to return a value from this function.

This function must only be used by functions that either have a @greenado.groutine decorator, or functions that are children of functions that have the decorator applied.

New in version 0.1.7.

Warning

You should not discard the returned Future or exceptions may be silently discarded, similar to a tornado coroutine. See @gen.coroutine for details.

greenado.concurrent.gmoment()[source]

Similar to tornado.gen.moment(), yields the IOLoop for a single iteration from inside a groutine.

greenado.concurrent.groutine(f)[source]

A decorator that makes a function asynchronous and returns the result of the function as a tornado.concurrent.Future. The wrapped function may use gyield() to pseudo-synchronously wait for a future to resolve.

The primary advantage to using this decorator is that it allows all called functions and their children to use gyield(), and doesn’t require the use of generators.

If you are calling a groutine-wrapped function from a function with a @greenado.groutine decorator, you will need to use gyield() to wait for the returned future to resolve.

From a caller’s perspective, this decorator is functionally equivalent to the @gen.coroutine decorator. You should not use this decorator and the @gen.coroutine decorator on the same function.

Warning

You should not discard the returned Future or exceptions may be silently discarded, similar to a tornado coroutine. See @gen.coroutine for details.

greenado.concurrent.gsleep(timeout)[source]

This will yield and allow other operations to occur in the background before returning.

Parameters:timeout – Number of seconds to wait

New in version 0.1.9.

greenado.concurrent.gyield(future, timeout=None)[source]

This is functionally equivalent to the ‘yield’ statements used in a @gen.coroutine, but doesn’t require turning all your functions into generators – so you can use the return statement normally, and exceptions won’t be accidentally discarded.

This can be used on any function that returns a future object, such as functions decorated by @gen.coroutine, and most of the tornado API as of tornado 4.0.

This function must only be used by functions that either have a @greenado.groutine decorator, or functions that are children of functions that have the decorator applied.

Parameters:
Returns:

The result set on the future object

Raises:
  • If an exception is set on the future, the exception will be thrown to the caller of gyield.
  • If the timeout expires, TimeoutError will be raised.

Changed in version 0.1.8: Added timeout parameter

Changed in version 0.2.0: If a timeout occurs, the TimeoutError will not be set on the future object, but will only be raised to the caller.

greenado.testing

greenado.testing.gen_test(func=None, timeout=None)[source]

An implementation of tornado.testing.gen_test() for @greenado.groutine

Example:

def something_that_yields():
    greenado.gyield(something())

class MyTest(AsyncTestCase):
    @greenado.testing.gen_test
    def test_something(self):
        something_that_yields()