yappi icon indicating copy to clipboard operation
yappi copied to clipboard

Please add context-manager for easier of use

Open alex-ber opened this issue 1 year ago • 1 comments
trafficstars

This will be less error-prone for your clients. This is my implementation.

import yappi
import contextlib


@contextlib.contextmanager
def yappi_profiler(stats_file_path, stats_format_type='callgrind'):
    yappi.start()
    try:
        yield
    finally:
        yappi.stop()
        yappi.get_func_stats() \
            .save(stats_file_path, type=stats_format_type)
        yappi.clear_stats()


@contextlib.asynccontextmanager
async def ayappi_profiler(stats_file_path, stats_format_type='callgrind):
    yappi.start()
    try:
        yield
    finally:
        yappi.stop()
        yappi.get_func_stats() \
            .save(stats_file_path, type=stats_format_type)
        yappi.clear_stats()

alex-ber avatar Jul 11 '24 12:07 alex-ber

I am up for it if you can implement this + some tests around this. There might be some naming changes required as well since there is also get_thread_stats..etc.

sumerc avatar Jul 19 '24 12:07 sumerc