Are coroutines supported? #267
-
Can memray be used for applications that uses asyncio? does it trace async/await functions properly? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Coroutines are supported, but the way they are reported may be surprising. Internally, the way that coroutines work is by yielding control back to an event loop when control is yielded from one coroutine to another, so in the implementation, coroutines are directly called by the event loop. The stacks that Memray shows for coroutines reflect that reality. Our flame graphs may not show which coroutine awaited another coroutine, and can instead show which runner ran each coroutine. This accurately reflects the reality of how coroutines get run, but the lack of context about why the runner is running a particular coroutine can make it hard to reason about the reports. For instance, this flame graph: Is the result of running this Python program: import asyncio
xs = []
async def foo():
return await bar()
async def bar():
return await baz()
async def baz():
await asyncio.sleep(0.1)
xs.append("x" * 1024 * 1024)
async def main():
print(await asyncio.gather(foo(), bar(), baz()))
asyncio.run(main()) We do show 3 different call stacks that all lead to |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for the detailed response! |
Beta Was this translation helpful? Give feedback.
-
Also there is an interesting case with async execution, such as with asyncio.create_task(I have a lot of this), which effect it will make? |
Beta Was this translation helpful? Give feedback.
Coroutines are supported, but the way they are reported may be surprising. Internally, the way that coroutines work is by yielding control back to an event loop when control is yielded from one coroutine to another, so in the implementation, coroutines are directly called by the event loop. The stacks that Memray shows for coroutines reflect that reality. Our flame graphs may not show which coroutine awaited another coroutine, and can instead show which runner ran each coroutine. This accurately reflects the reality of how coroutines get run, but the lack of context about why the runner is running a particular coroutine can make it hard to reason about the reports.
For instance, this flam…