Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Better document @profile usage #18

Closed
dellis23 opened this issue Jan 16, 2015 · 19 comments
Closed

Better document @profile usage #18

dellis23 opened this issue Jan 16, 2015 · 19 comments

Comments

@dellis23
Copy link

It's unclear to me as to whether this is required, but after running without using this decorator anywhere, I got no results, so I'm assuming it is. I tried importing line_profiler in ipython and seeing if the decorator was there, but I didn't see it. I'm digging through the code now to see if I can figure it out.

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

Yes, it's always required. Does this new wording help?

aa1f425

@dellis23
Copy link
Author

Well, my bigger issue right now is that I'm not sure how to actually import it. import profile doesn't work. I tried instantiating LineProfiler and using it as a decorator, but it gave no results. I tried importing __builtin__ and running kernprof with -b but I still just get this as the output when viewing with line_profiler:

Timer unit: 1e-06 s

@dellis23
Copy link
Author

But yeah that new wording definitely makes it clear I need something, just not sure what yet.

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

You don't instantiate it. Running kernprof -b myscript.py injects it into the builtins.

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

Sorry, -b is for standard profiling. -l injects the LineProfiler into the builtins (search for "builtins" in the README).

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

The easiest way to
get started is to use the `kernprof` script. ::

    $ kernprof -l script_to_profile.py

`kernprof` will create an instance of LineProfiler and insert it into the
`__builtins__` namespace with the name `profile`. It has been written to be
used as a decorator, so in your script, you decorate the functions you want
to profile with @profile. ::

    @profile
    def slow_function(a, b, c):
        ...

@dellis23
Copy link
Author

Hmm. Perhaps it just isn't working for me then, and isn't a documentation issue:

blah@dan:~/myapp$ kernprof -l app.py -b
Traceback (most recent call last):
  File "app.py", line 107, in <module>
    @profile
NameError: name 'profile' is not defined
Wrote profile results to app.py.lprof

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

Is the -b for your app.py?

@dellis23
Copy link
Author

No, it doesn't take any arguments. Without the -b gives the same issue. If no one else is experiencing this maybe it's some edge case with my setup (though I can't imagine what that might be). I can close the issue unless you want to keep exploring it; I know this isn't tech support and I don't want to waste your time.

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

Can you upload (gist) an app.py that is causing problems?

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

Do omit the -b. It is mutually exclusive with -l.

@dellis23
Copy link
Author

It would take me too long to redact all the sensitive stuff, but here's a smaller version that recreates the same issue: https://gist.github.com/dellis23/d5251031787ff370f15f

And the output:

blah@dan:~/testflask$ kernprof -l app.py 
 * Running on http://127.0.0.1:3000/
 * Restarting with reloader
162.222.72.1 - - [16/Jan/2015 10:34:28] "GET / HTTP/1.1" 200 -
^CWrote profile results to app.py.lprof
blah@dan:~/testflask$ python -m line_profiler app.py.lprof
Timer unit: 1e-06 s

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

There's no @profile in there. Are you trying to combine it with the @app.route('/') decorator? For various reasons, that may or may not work.

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

You have some options, like refactoring the body of the function to a function all by itself, @profile that one, and have the @app.routed function call that one.

Also try a non-Flask script to see if line_profiler is just totally broken on your system or if it's just not working with Flask.

@dellis23
Copy link
Author

Doh, yeah, I put the @profile in and had the same results. But I think you're right, it's either a Flask issue or a problem with the multiple decorators. Running a small, non-flask dummy script confirmed that it's not just my system.

Thanks for the help -- hopefully it at least guides someone else in the right direction if they are trying it with Flask.

@rkern
Copy link
Owner

rkern commented Jan 16, 2015

Thanks. I've added an issue to document the issues with multiple decorators. #19

@stickytruth
Copy link

This seems to be an issue with flask's reloader, rather than an issue of multiple decorators. Also, Flask's documentation mentions "When applying further decorators, always remember that the route() decorator is the outermost"

An example of kernprof working with flask can be seen in this gist: https://gist.github.com/stickytruth/39c3fa2c88f141a0a26b

@rkern
Copy link
Owner

rkern commented Mar 12, 2015

Ah, good detective work! Thanks!

@Downchuck
Copy link

I ran into this issue in virtualenv -- are there issues there?

NameError: name 'profile' is not defined

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants