Python 3.4+Async programmingfunction
asyncio.iscoroutine()
introspection
Checks whether an object is a coroutine object, i.e. the result of calling an async function rather than the function itself.
Syntax
asyncio.iscoroutine(obj)
A common mix-up: iscoroutine(fetch) is False while iscoroutine(fetch()) is True. Calling fetch() runs nothing, it only creates the object; if you neither await nor close it, Python emits RuntimeWarning "coroutine was never awaited". To test a function use inspect.iscoroutinefunction, and for "can I await this" use inspect.isawaitable: a Task or a Future is not a coroutine.
Examples
import asyncio
async def fetch():
return 1
coro = fetch()
print(asyncio.iscoroutine(fetch)) # → False — это функция
print(asyncio.iscoroutine(coro)) # → True
coro.close() # иначе RuntimeWarning: coroutine was never awaited
import asyncio
async def main():
task = asyncio.create_task(asyncio.sleep(0.01))
print(asyncio.iscoroutine(task)) # → False — задача awaitable, но не корутина
await task
asyncio.run(main())
See also
Open in the full glossarydocs.python.orgReport an inaccuracy