views:

14

answers:

1

Is there a way to run cProfile or line_profile on a script on a server?

ie: how could I get the results for one of the two methods on http://www.Example.com/cgi-bin/myScript.py

Thanks!

+1  A: 

Not sure what line_profile is. For cProfile, you just need to direct the results to a file you can later read on the server (depending on what kind of access you have to the server).

To quote the example from the docs,

import cProfile
cProfile.run('foo()', 'fooprof')

and put all the rest of the code into a def foo(): -- then later retrieve that fooprof file and analyze it at leisure (assuming your script runs with permissions to write it in the first place, of course).

Of course you can ensure different runs get profiled into different files, etc, etc -- whether this is practical also depends on what kind of access and permissions you're getting from your hosting provider, i.e., how are you allowed to persist data, in a way that lets you retrieve that data later? That's not a question of Python, it's a question of contracts between you and your hosting provider;-).

Alex Martelli
Yeah, I saw that in the docs, so would I have to make my entire script one big function?
Parker
@Parker, it's _always_ best to have all your "meaningful" code in functions (one, or, preferably, more, that get all called from a central one;-) rather than at module top-level -- that enables a simple but crucial optimization which the Python compiler transparently performs for you (making variable references into very fast indexings into an internal array instead of dict lookups, if you must know;-); that, per se, will buy you a nice performance boost "basically for free", even before you start profiling;-).
Alex Martelli
Thanks Alex! I didn't realize that functions would speed things up. Is that the case even for ones that I only use once?
Parker
@Parker, yes, compared to module-level code, especially if some variables get used a lot (e.g. in `for i in range(10): `, `i` gets set 10 times -- each time there will be a saving if `i` is a local variable of a function, compared to it being a module-level one).
Alex Martelli