Skip to content Skip to sidebar Skip to footer

Why Isn't A Function Declared As Async Of Type Types.coroutinetype?

Quote from here: types.CoroutineType The type of coroutine objects, created by async def functions. Quote from here: Functions defined with async def syntax are always coroutine

Solution 1:

There's a difference between coroutine and coroutine function. The same way as there is a difference between generator and generator function:

Calling the function g returns a coroutine, e.g.:

>>> isinstance(g(), types.CoroutineType)
True

If you need to tell if g is a coroutine function (i.e. would return a coroutine) you can check with:

>>>from asyncio import iscoroutinefunction>>>iscoroutinefunction(g)
True

Solution 2:

g by itself is not a valid Coroutine function when used that way:

isinstance(g, types.CoroutineType)

This is similar to the difference between a generator and a generator function. Instead, use g() to compare:

isinstance(g(), types.CoroutineType)

You could also try iscoroutinefunction(g) instead, much more shorter and neater:

from asyncio import iscoroutinefunctioniscoroutinefunction(g)   #Returntrue

Read more here: https://docs.python.org/3/library/asyncio-task.html

Post a Comment for "Why Isn't A Function Declared As Async Of Type Types.coroutinetype?"