Python 连续调用cProfile/pstats无法正确更新

Python 连续调用cProfile/pstats无法正确更新,python,Python,我试图连续调用一些分析器代码,但是在第二次调用函数时,概要文件的更新时间会更改,但实际的分析器统计数据保持不变。这不是我正在运行的代码,但它是一个我能想到的显示相同行为的简化示例 在运行时,第一次按下ctrl+c时,它会显示统计信息,第二次显示的是相同的信息,但并没有像预期的那样完全更新,只有时间是,第三次程序实际退出。如果尝试,最好在按下ctrl+c键之间等待几秒钟 在第8行之后添加profiler.enable()确实会在调用之间提供完整的更新,但是它会为我不想分析的内容添加大量额外的pro

我试图连续调用一些分析器代码,但是在第二次调用函数时,概要文件的更新时间会更改,但实际的分析器统计数据保持不变。这不是我正在运行的代码,但它是一个我能想到的显示相同行为的简化示例

在运行时,第一次按下ctrl+c时,它会显示统计信息,第二次显示的是相同的信息,但并没有像预期的那样完全更新,只有时间是,第三次程序实际退出。如果尝试,最好在按下ctrl+c键之间等待几秒钟

在第8行之后添加profiler.enable()确实会在调用之间提供完整的更新,但是它会为我不想分析的内容添加大量额外的profiler数据

有没有关于快乐媒体的建议,在那里我可以得到完整的更新,但没有额外的绒毛

import signal, sys, time, cProfile, pstats

call = 0

def sigint_handler(signal, frame):
global call
if call < 2:
    profiler.dump_stats("profile.prof")
    stats = pstats.Stats("profile.prof")
    stats.strip_dirs().sort_stats('cumulative').print_stats()
    call += 1
else:
    sys.exit()
def wait():
time.sleep(1)

def main_io_loop():
signal.signal(signal.SIGINT, sigint_handler)
while 1:
    wait()

profiler = cProfile.Profile()
profiler.runctx("main_io_loop()", globals(), locals())
导入信号、系统、时间、cProfile、pstats
呼叫=0
def信号处理器(信号,帧):
全球电话
如果呼叫<2:
profile.dump_stats(“profile.prof”)
stats=pstats.stats(“profile.prof”)
stats.strip_dirs().sort_stats('cumulative').print_stats()
呼叫+=1
其他:
sys.exit()
def wait():
时间。睡眠(1)
def main_io_loop():
signal.signal(signal.SIGINT,SIGINT_处理程序)
而1:
等等
profiler=cProfile.Profile()
runctx(“main\u io\u loop()”,globals(),locals())

调用profiler.dump_stats(在cProfile.py中实现)调用profiler.create_stats,后者依次调用profiler.disable()

您需要调用profiler.enable()使其再次工作。不,这没有记录在案

以下内容似乎符合您的要求。注意,我去掉了中间数据文件,因为pstats.Stats知道如何直接从分析器获取数据

import signal, sys, time, pstats, cProfile

call = 0

def sigint_handler(signal, frame):
  global call
  if call < 2:
    stats = pstats.Stats(profiler)
    stats.strip_dirs().sort_stats('cumulative').print_stats()
    profiler.enable()
    call += 1
  else:
    sys.exit()

def wait():
  time.sleep(1)

def main_io_loop():
  signal.signal(signal.SIGINT, sigint_handler)
  while 1:
    wait()

profiler = cProfile.Profile()
profiler.runctx("main_io_loop()", globals(), locals())
导入信号、系统、时间、pstats、cProfile
呼叫=0
def信号处理器(信号,帧):
全球电话
如果呼叫<2:
stats=pstats.stats(分析器)
stats.strip_dirs().sort_stats('cumulative').print_stats()
profiler.enable()
呼叫+=1
其他:
sys.exit()
def wait():
时间。睡眠(1)
def main_io_loop():
signal.signal(signal.SIGINT,SIGINT_处理程序)
而1:
等等
profiler=cProfile.Profile()
runctx(“main\u io\u loop()”,globals(),locals())

调用profiler.dump_stats(在cProfile.py中实现)调用profiler.create_stats,后者依次调用profiler.disable()

您需要调用profiler.enable()使其再次工作。不,这没有记录在案

以下内容似乎符合您的要求。注意,我去掉了中间数据文件,因为pstats.Stats知道如何直接从分析器获取数据

import signal, sys, time, pstats, cProfile

call = 0

def sigint_handler(signal, frame):
  global call
  if call < 2:
    stats = pstats.Stats(profiler)
    stats.strip_dirs().sort_stats('cumulative').print_stats()
    profiler.enable()
    call += 1
  else:
    sys.exit()

def wait():
  time.sleep(1)

def main_io_loop():
  signal.signal(signal.SIGINT, sigint_handler)
  while 1:
    wait()

profiler = cProfile.Profile()
profiler.runctx("main_io_loop()", globals(), locals())
导入信号、系统、时间、pstats、cProfile
呼叫=0
def信号处理器(信号,帧):
全球电话
如果呼叫<2:
stats=pstats.stats(分析器)
stats.strip_dirs().sort_stats('cumulative').print_stats()
profiler.enable()
呼叫+=1
其他:
sys.exit()
def wait():
时间。睡眠(1)
def main_io_loop():
signal.signal(signal.SIGINT,SIGINT_处理程序)
而1:
等等
profiler=cProfile.Profile()
runctx(“main\u io\u loop()”,globals(),locals())

谢谢,在print_stats()之后启用探查器可以防止探查器被我在启用()之前提到的“绒毛”污染。还感谢您提供的将分析器直接传递到统计信息的提示。这一次我需要这个文件,但我以后会记住的。我第一次读它的时候不明白你对fluff的评论。我以前从未使用过“启用”。我想知道您是否内嵌了替代代码以及注释,这是否会使我更容易复制这些绒毛。谢谢,在print_stats()之后启用探查器可以防止探查器被我在之前放置enable()时提到的“绒毛”污染。还感谢您提供的将分析器直接传递到统计信息的提示。这一次我需要这个文件,但我以后会记住的。我第一次读它的时候不明白你对fluff的评论。我以前从未使用过“启用”。我想知道您是否嵌入了替代代码内联,以及一条注释,这是否会使我更容易重现这些绒毛。