Python Glossary Русский

Python 3.5+inspectfunction

inspect.iscoroutine()

coroutines and generators

Checks that an object is a coroutine created by calling an async def function; not to be confused with the function itself.

Syntax

inspect.iscoroutine(object) -> bool

The check is narrow: native coroutines only. A @types.coroutine generator, a Future and an object with __await__ give False although you can await them; isawaitable answers that question. A coroutine that was created but never run must be awaited or closed (.close()), otherwise Python warns 'coroutine was never awaited'.

Examples

import inspect
async def job():
    return 1
class Waiter:
    def __await__(self):
        yield
c = job()
print(inspect.iscoroutine(c), inspect.iscoroutine(job))  # → True False
print(inspect.iscoroutine(Waiter()), inspect.isawaitable(Waiter()))  # → False True
c.close()  # не запущенную корутину закрываем

See also

Open in the full glossarydocs.python.orgReport an inaccuracy