[Python-ideas] Allow Profile() to be used as a context manager
David Townshend
aquavitae69 at gmail.com
Wed Jan 25 10:47:54 CET 2012
On Wed, Jan 25, 2012 at 10:19 AM, Yuval Greenfield <ubershmekel at gmail.com>wrote:
> On Wed, Jan 25, 2012 at 8:36 AM, David Townshend <aquavitae69 at gmail.com>wrote:
>
>> [...]
>> Any major objections to this?
>>
>>
>
> At first this sounded like a good idea but 2 things pop to mind:
>
>
> 1. How does one get the pstat profiling output from the context
> manager? Will printing out the results be the only option?
>
> The same way as currently:
profile = cProfile.Profile()
with profile:
do_something()
profile.dump_stats(filename)
Perhaps it would be a good idea to provide a convenience function (like
run()), so that:
with cProfile.do(filename):
do_something()
I don't like calling the function do(), but I can't think of anything
better off-hand, and it illustrates my point.
>
> 1. Usually when I profile I don't want to touch the code at all so
> eventually if I'll use this context manager it'll be more akin to
>
>
> with profile.Profiler():
> main()
>
> Which isn't much of a win over
>
> profile.run('main()')
>
I agree, but this isn't the use case.
> Now if you're talking about a reentrant profiler, that's interesting:
>
> profiler = profile.Profiler()
>
> while is_working:
> with profiler:
> do_hard_task()
>
> do_a_hard_but_i_dont_care_now_task()
>
> with profiler:
> do_a_diff_hard_task()
>
>
> and then I can call whatever method is there to get the pstat. If this is
> the vision then it sounds awesome.
>
>
And this is the use case! I'm not entirely sure how re-entrancy would be
dealt with by the profiler though. From what I read in the source code it
looks like it would all accumulate, and this is certainly the way I would
expect it to behave, but I'd need to run a few tests to be sure.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120125/75180ebc/attachment.html>
More information about the Python-ideas
mailing list