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

Giampaolo Rodolà g.rodola at gmail.com
Wed Jan 25 10:40:16 CET 2012

Il 25 gennaio 2012 07:36, David Townshend <aquavitae69 at gmail.com> ha scritto:
> 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?

Related: http://bugs.python.org/issue9285

--- Giampaolo

More information about the Python-ideas mailing list