25

Consider a coroutine which calls into another coroutine:

async def foo(bar):
     result = await bar()
     return result

This works fine if bar is a coroutine. What do I need to do (i.e. with what do I need to wrap the call to bar) so that this code does the right thing if bar is a normal function?

It is perfectly possible to define a coroutine with async def even if it never does anything asynchronous (i.e. never uses await). However, the question asks how to wrap/modify/call a regular function bar inside the code for foo such that bar can be awaited.

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
DanielSank
  • 3,303
  • 3
  • 24
  • 42

1 Answers1

22

Simply wrap your synchronous function with asyncio.coroutine if needed:

if not asyncio.iscoroutinefunction(bar):
    bar = asyncio.coroutine(bar)

Since it is safe to re-wrap a coroutine, the coroutine function test is actually not required:

async_bar = asyncio.coroutine(sync_or_async_bar)

Therefore, your code can be re-written as follows:

async def foo(bar):
     return await asyncio.coroutine(bar)()
Vincent
  • 12,919
  • 1
  • 42
  • 64
  • 1
    This appears to work. Note, interestingly, that `asyncio.coroutine(normal_function)()` is a generator, while `a_coroutine_function()` is a coroutine. – DanielSank Jul 25 '16 at 10:16
  • 2
    Is there any way to treat the generator object this returns like a Task object, to call `Task.done()` and see if it is still running for example? – Dagrooms Jan 31 '18 at 21:35
  • 24
    [*Deprecated since version 3.8, will be removed in version 3.10*](https://docs.python.org/3/library/asyncio-task.html#asyncio.coroutine). The documentation says to use `async def` instead, but what is the future-proof method of turning a function into a coroutine? – gerrit Apr 16 '20 at 15:09