views:

36

answers:

1

Hey guys,

Sorry for the beginner question, but I can't figure out cProfile (I'm really new to Python)

I can run it via my terminal with:

python -m cProfile myscript.py

But I need to run it on a webserver, so I'd like to put the command within the script it will look at. How would I do this? I've seen stuff using terms like __init__ and __main__ but I dont really understand what those are.

I know this is simple, I'm just still trying to learn everything and I know there's someone who will know this.

Thanks in advance! I appreciate it.

+3  A: 

I think you've been seeing ideas like this:

if __name__ == "__main__":
    # do something if this script is invoked
    # as python scriptname. Otherwise, gets ignored.

What happens is when you call python on a script, that file has an attribute __name__ set to "__main__" if it is the file being directly called by the python executable. Otherwise, (if it is not directly called) it is imported.

Now, you can use this trick on your scripts if you need to, for example, assuming you have:

def somescriptfunc():
    # does something
    pass


if __name__ == "__main__":
    # do something if this script is invoked
    # as python scriptname. Otherwise, gets ignored.

    import cProfile
    cProfile.run('somescriptfunc()')

This changes your script. When imported, its member functions, classes etc can be used as normal. When run from the command-line, it profiles itself.

Is this what you're looking for?


From the comments I've gathered more is perhaps needed, so here goes:

If you're running a script from CGI changes are it is of the form:

# do some stuff to extract the parameters
# do something with the parameters
# return the response.

When I say abstract out, you can do this:

def do_something_with_parameters(param1, param2):
    pass

if __name__ = "__main__":
    import cProfile
    cProfile.run('do_something_with_parameters(param1=\'sometestvalue\')')

Put that file on your python path. When run itself, it will profile the function you want profiling.

Now, for your CGI script, create a script that does:

import {insert name of script from above here}

# do something to determine parameter values
# do something with them *via the function*:
do_something_with_parameters(param1=..., param2=...)
# return something

So your cgi script just becomes a little wrapper for your function (which it is anyway) and your function is now self-testing.

You can then profile the function using made up values on your desktop, away from the production server.

There are probably neater ways to achieve this, but it would work.

Ninefingers
Yes! Thanks. What you typed though would just have cProfile look at somescriptfunc() right? How can I make it look at the entire .py file though?
Parker
Correct. I'm not sure how you'd do that - what I'd do is have a function that did everything I needed profiling, or else run `cProfile.run` on each of the functions that needed profiling. The script shouldn't be being called directly by an interpreter. If you are doing that, you will profile when you execute the script in CGI form.
Ninefingers
Yeah, I'm running the script from CGI. Like how would I get a cProfile output on http://example.com/cgi-bin/myscript.py?val=someVal
Parker
You can't. At least not directly, as far as I know. You need to abstract out the functionality you want to profile such that you can wrap it up in some dummy data using another script and test that.
Ninefingers
Would that be easy to do? And I could just hardcode the values in, so I just need to run it from a website as opposed to on my computer.
Parker
Yep. Added to my answer because it is easier to explain that way.
Ninefingers
I got it! Thanks!
Parker