2

I'd like to check if a function is async before calling it in python. For example, here I'd like to check if f is async so that await makes sense:

async def call_async_f(f):
    assert function_is_async(f)
    await f()

How could I implement function_is_async? I'm using python 3.7 which seems to have some interesting new async features and I don't mind a 3.7-specific solution.

Mei Zhang
  • 1,434
  • 13
  • 29

1 Answers1

3

inspect has checks for most types of async objects, in your case it`s iscoroutinefunction

import inspect
async def call_async_f(f):
    assert inspect.iscoroutinefunction(f)
    await f()
Trizalio
  • 811
  • 7
  • 16