Python Glossary Русский

Python 3.5+collectionsterm

collections.abc.Coroutine

collections.abc — protocols

The coroutine protocol: an awaitable with send/throw/close.

Syntax

isinstance(obj, collections.abc.Coroutine)

Calling an async function runs no code at all: it just builds a coroutine object, and if nobody awaits it or hands it to asyncio, the body never executes and the interpreter emits a "coroutine was never awaited" RuntimeWarning when the object is collected. Coroutine is narrower than Awaitable — Tasks and Futures can be awaited but fail this check, so test against Awaitable when the question is merely "can I await this?".

Examples

import collections.abc as abc
async def f(): pass
c = f()
print(isinstance(c, abc.Coroutine))   # → True
c.close()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy