Python 3.7+sysfunction
sys.set_coroutine_origin_tracking_depth()
asynchronous code
Enables recording where coroutines are created: this many stack frames go into cr_origin. 0 turns it off. Helps find a forgotten await.
Syntax
sys.set_coroutine_origin_tracking_depth(depth)
The 'coroutine ... was never awaited' warning alone does not say where the coroutine was created; with a depth above zero it shows that place. asyncio debug mode (asyncio.run(main(), debug=True) or PYTHONASYNCIODEBUG=1) enables it for you. The depth is per thread and slows down creating every coroutine; a negative value raises ValueError.
Examples
import sys
async def job():
return 1
sys.set_coroutine_origin_tracking_depth(1)
c = job()
print(c.cr_origin[0][1:]) # → (5, '<module>') — строка и функция создания
c.close()
sys.set_coroutine_origin_tracking_depth(0)
print(sys.get_coroutine_origin_tracking_depth()) # → 0
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy