def toggle_profiling(self, *args):
    if self.prof:
        self.prof.disable()
        filename = f'callback-{self.pid}.pstats'
        filepath = os.path.join(tempfile.gettempdir(), filename)
        with open(filepath, 'w') as f:
            pstats.Stats(self.prof, stream=f).sort_stats('cumulative').print_stats()
        pstats.Stats(self.prof).dump_stats((filepath + '.raw'))
        self.prof = False
        logger.error(f'profiling is disabled, wrote {filepath}')
    else:
        self.prof = cProfile.Profile()
        self.prof.enable()
        logger.error('profiling is enabled')
def toggle_profiling(self, *args):
    if self.prof:
        self.prof.disable()
        filename = f'callback-{self.pid}.pstats'
        filepath = os.path.join(tempfile.gettempdir(), filename)
        with open(filepath, 'w') as f:
            pstats.Stats(self.prof, stream=f).sort_stats('cumulative').print_stats()
        pstats.Stats(self.prof).dump_stats((filepath + '.raw'))
        self.prof = False
        logger.error(f'profiling is disabled, wrote {filepath}')
    else:
        self.prof = cProfile.Profile()
        self.prof.enable()
        logger.error('profiling is enabled')