qtconsole icon indicating copy to clipboard operation
qtconsole copied to clipboard

Profiling once active/enabled cannot be deactivated/disabled

Open misyntropy opened this issue 1 year ago • 6 comments

In my script, I use profiling to keep track:

if __name__ == '__main__':
    pr = Profile()
    pr.enable()
    #...
    #My code
    #...
    pr.disable()
    s = io.StringIO()
    sortby = pstats.SortKey.CUMULATIVE
    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    ps.print_stats()
    ps.dump_stats("../run.prof")

If I encounter an error and this script doesn't run to completion, and I try to run again (after fixing the problem but that's not required here), I encounter this error at the pr.enable() line.

ValueError: Another profiling tool is already active

At this point, even if I run pr.disable() or even del pr, this error never goes away. The only thing that works is to restart the kernel itself.

Is this a bug? Is this a bad practice that I should not do (if so, what's the standard practice?)?

misyntropy avatar Jul 24 '24 19:07 misyntropy

Okay I skipped a couple of steps and hence may have filed this in the wrong location - I'm following the Spyder troubleshooting guide and I first got it in Spyder. Then I tried qtconsole and got the error there as well, which is why I opened this issue.

But just now I tried it in IPython and again got the error. So now I ran it in the Python interpreter using exec(open('mainrunner.py').read()) and still get the same problem - the profiling state cannot be disabled, reset, removed or anything else seemingly. Obviously running an entirely different instance of Python with python mainrunner.py does not show this problem.

What should I do? It feels like I'm doing something wrong here.

misyntropy avatar Jul 24 '24 20:07 misyntropy

Hello? Bump!

misyntropy avatar Jan 14 '25 12:01 misyntropy

I think the only solution is to restart the kernel after fixing the error to get rid of the previous profiling session and start all over again.

ccordoba12 avatar Jan 14 '25 16:01 ccordoba12

@ccordoba12 This sounds bad — what is the point of the disable method then? Where does the profiling data burrow itself —which seems to be a pretty deep place — that restarting the kernel is the only option??

misyntropy avatar Jan 14 '25 17:01 misyntropy

I don't know what the disable method does or doesn't do, but the error you posted is quite clear:

ValueError: Another profiling tool is already active

As long as there is a profiling session active, you can't start another one. And the only way to do that is by restarting the kernel, I guess.

ccordoba12 avatar Jan 15 '25 17:01 ccordoba12

Noted.

Since it sounds pretty wrong to me, where would you recommend I go next to resolve ambiguity in the "I guess" part of your answer?

https://github.com/ipython/ipython ? https://github.com/python/cpython ?

Thanks!

misyntropy avatar Jan 15 '25 17:01 misyntropy