Python 3.4+until 3.16Async programmingfunction
asyncio.iscoroutinefunction()
introspection
Deprecated check that a function is declared with async def; since 3.14 it emits DeprecationWarning, removal in 3.16. Use inspect.iscoroutinefunction() instead.
Syntax
inspect.iscoroutinefunction(func) # вместо asyncio.iscoroutinefunction(func)
The function lived in asyncio for the old generator-based @asyncio.coroutine (removed in 3.11), was never in the module reference, and was deprecated in 3.14. inspect.iscoroutinefunction does the same job and also sees through functools.partial; a plain function returning an awaitable can be marked with inspect.markcoroutinefunction (3.12+). Both look at the declaration, not the behaviour: a def that returns a coroutine still gives False.
Examples
import inspect
async def fetch():
return 1
def wrapper():
return fetch()
print(inspect.iscoroutinefunction(fetch)) # → True
print(inspect.iscoroutinefunction(wrapper)) # → False — смотрит на объявление
import asyncio
import warnings
async def fetch():
return 1
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning) # с 3.14 здесь DeprecationWarning
print(asyncio.iscoroutinefunction(fetch)) # → True
# Python 3.16+
import functools
import inspect
async def fetch(x):
return x
print(inspect.iscoroutinefunction(functools.partial(fetch, 1))) # → True — partial тоже
See also
Open in the full glossarydocs.python.orgReport an inaccuracy