0

As mentioned in the title, what is the asyncio equivalent of Twisted's defer.succeed? Alternatively how do I create a coroutine object from a plain Python value?

Note that this is different from How do we call a normal function where a coroutine is expected?. I could always do something like the following but I was wondering if there was a better way.

value_to_convert_to_coroutine_object = 5
async def foo(bar):
     return await asyncio.coroutine(lambda: value_to_convert_to_coroutine_object)()
Kent Shikama
  • 3,910
  • 3
  • 22
  • 55

1 Answers1

0

defer.succeed() is nothing fancy and, as far as I know, there's not single asyncio function that replicates that.

def succeed(result):
    f = Future()
    f.set_result(result)
    f.done()
    return f

I recall on an asyncio IRC, that the authors didn't want to make functions that the user could simply write out on their own. Which might be why such a function doesn't exist.

As for the second part of your question, it seems like the solution you have is as simple as it gets. I've done something similar for unit tests.

notorious.no
  • 4,919
  • 3
  • 20
  • 34