Documentations say:
@asyncio.coroutine
Decorator to mark generator-based coroutines. This enables the generator use yield from to call async def coroutines, and also enables the generator to be called by async def coroutines, for instance using an await expression.
_
@types.coroutine(gen_func)
This function transforms a generator function into a coroutine function which returns a generator-based coroutine. The generator-based coroutine is still a generator iterator, but is also considered to be a coroutine object and is awaitable. However, it may not necessarily implement the
__await__()
method.
So is seems like purposes is the same - to flag a generator as a coroutine (what async def
in Python3.5 and higher does with some features).
When need to use asyncio.coroutine
when need to use types.coroutine
, what is the diffrence?
asyncio
is a codebase that has to be compatible with earlier Python versions, whiletypes
is only distributed with Python itself. So you could see theasyncio.coroutine()
as a backport oftypes.coroutine()
that can be used in other Python versions. – Martijn Pieters♦