[Python-ideas] Allow Profile() to be used as a context manager

David Townshend aquavitae69 at gmail.com
Wed Jan 25 11:28:46 CET 2012


Would that stop the profiler between calles though?  In Yuval's example,
wouldn't  do_a_hard_but_i_dont_care_now_task be included in the profile
then?

On Wed, Jan 25, 2012 at 12:16 PM, Robert Kern <robert.kern at gmail.com> wrote:

> On 1/25/12 6:36 AM, David Townshend wrote:
>
>> I often find it useful to profile small sections to code in a running
>> application to pinpoint a known bottleneck. The cProfile and profile
>> modules
>> don't make this easy as they stand, requiring the section of code to be
>> wrapped
>> into a single statement with can be called as a string using exec().  This
>> always feels a bit clumsy to me, so my idea is simply to provide
>> __enter__ and
>> __exit__ methods to cProfile.Profile() and profile.Profile().  From a
>> quick look
>> at the code it seems that this should be very easy for cProfile, and
>> slightly
>> less easy for profile (I can't quite figure out how to deal with the
>> set_cmd()
>> line, or how important it is - maybe someone can help?).
>>
>> Any major objections to this?
>>
>
> I've done this in my kernprof.py script, which I use as a convenient
> generic profiling script. I subclass cProfile.Profile to add
> enable_by_count()/disable_by_**count() methods that allow nesting.
> enable_by_count() increments a counter and only calls the enable() method
> the first time. disable_by_count() decrements the counter and only calls
> disable() when it hits 0 again. __enter__() and __exit__() just call these
> methods. I also add a __call__() method that lets a Profile instance act as
> a decorator, which I actually find somewhat more useful than the context
> manager.
>
> https://bitbucket.org/**robertkern/line_profiler/src/**
> tip/kernprof.py#cl-57<https://bitbucket.org/robertkern/line_profiler/src/tip/kernprof.py#cl-57>
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma
>  that is made terrible by our own mad attempt to interpret it as though it
> had
>  an underlying truth."
>  -- Umberto Eco
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120125/cac58fac/attachment.html>


More information about the Python-ideas mailing list