def chain_return_value(future, loop, return_value):
    'Compatible way to return a value in all Pythons.\n\n    PEP 479, raise StopIteration(value) from a coroutine won\'t work forever,\n    but "return value" doesn\'t work in Python 2. Instead, Motor methods that\n    return values resolve a Future with it, and are implemented with callbacks\n    rather than a coroutine internally.\n    '
    chained = concurrent.Future()

    def copy(_future):
        if chained.done():
            return
        if (_future.exception() is not None):
            chained.set_exception(_future.exception())
        else:
            chained.set_result(return_value)
    future.add_done_callback(functools.partial(loop.add_callback, copy))
    return chained
Compatible way to return a value in all Pythons.

PEP 479, raise StopIteration(value) from a coroutine won't work forever,
but "return value" doesn't work in Python 2. Instead, Motor methods that
return values resolve a Future with it, and are implemented with callbacks
rather than a coroutine internally.
def chain_return_value(future, loop, return_value):
    'Compatible way to return a value in all Pythons.\n\n    PEP 479, raise StopIteration(value) from a coroutine won\'t work forever,\n    but "return value" doesn\'t work in Python 2. Instead, Motor methods that\n    return values resolve a Future with it, and are implemented with callbacks\n    rather than a coroutine internally.\n    '
    chained = concurrent.Future()

    def copy(_future):
        if chained.done():
            return
        if (_future.exception() is not None):
            chained.set_exception(_future.exception())
        else:
            chained.set_result(return_value)
    future.add_done_callback(functools.partial(loop.add_callback, copy))
    return chained