def ensure_async(function_original):
    """
    A function decorated with this will always return a defer.Deferred
    even when returning synchronous result or raise an exception.
    """

    def wrapper(*args, **kwargs):
        try:
            result = function_original(*args, **kwargs)
            if isinstance(result, Deferred):
                return result
            d = Deferred()
            d.callback(result)
            return d
        except:
            return fail()

    return wrapper
def ensure_async ( function_original ) : def wrapper ( * args , ** kwargs ) : try : result = function_original ( * args , ** kwargs ) if isinstance ( result , Deferred ) : return result d = Deferred ( ) d . callback ( result ) return d except : return fail ( ) return wrapper
A function decorated with this will always return a defer.Deferred
    even when returning synchronous result or raise an exception.