Application Profiler¶
│ │ │This module provides a middleware that profiles each request with the
│ │ │ -cProfile
module. This can help identify bottlenecks in your code
│ │ │ +cProfile
module. This can help identify bottlenecks in your code
│ │ │ that may be slowing down your application.
-
│ │ │
-
│ │ │ class
werkzeug.middleware.profiler.
ProfilerMiddleware
(app, stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, sort_by=('time', 'calls'), restrictions=(), profile_dir=None, filename_format='{method}.{path}.{elapsed:.0f}ms.{time:.0f}.prof')¶
│ │ │ Wrap a WSGI application and profile the execution of each │ │ │ request. Responses are buffered so that timings are more exact.
│ │ │ -If
stream
is given,pstats.Stats
are written to it │ │ │ -after each request. Ifprofile_dir
is given,cProfile
│ │ │ +If
│ │ │stream
is given,pstats.Stats
are written to it │ │ │ +after each request. Ifprofile_dir
is given,cProfile
│ │ │ data files are saved to that directory, one file per request.The filename can be customized by passing
│ │ │filename_format
. If │ │ │ -it is a string, it will be formatted usingstr.format()
with │ │ │ +it is a string, it will be formatted usingstr.format()
with │ │ │ the following fields available:-
│ │ │
{method}
- The request method; GET, POST, etc.
│ │ │ {path}
- The request path or ‘root’ should one not exist.
│ │ │ {elapsed}
- The elapsed time of the request.
│ │ │ {time}
- The time of the request.
│ │ │
-
│ │ │
- Parameters │ │ │
-
│ │ │
app – The WSGI application to wrap.
│ │ │ stream – Write stats to this stream. Disable with
None
.
│ │ │ sort_by – A tuple of columns to sort stats by. See │ │ │ -
pstats.Stats.sort_stats()
.
│ │ │ +restrictions – A tuple of restrictions to filter stats by. See │ │ │ -
pstats.Stats.print_stats()
.
│ │ │ +profile_dir – Save profile data files to this directory.
│ │ │ filename_format – Format string for profile data file names, │ │ │ or a callable returning a name. See explanation above.
│ │ │
pstats.Stats.sort_stats()
. │ │ │pstats.Stats.print_stats()
. │ │ │
│ │ │
│ │ ││ │ │from werkzeug.middleware.profiler import ProfilerMiddleware │ │ ├── ./usr/share/doc/python-werkzeug-doc/html/middleware/shared_data.html │ │ │ @@ -72,15 +72,15 @@ │ │ │
│ │ ││ │ │app = SharedDataMiddleware(app, { │ │ │ '/static': ('myapplication', 'static') │ │ │ }) │ │ │
This will then serve the
│ │ │ -static
folder in the myapplication │ │ │ Python package.The optional disallow parameter can be a list of
fnmatch()
│ │ │ +The optional disallow parameter can be a list of
│ │ │fnmatch()
│ │ │ rules for files that are not accessible from the web. If cache is set to │ │ │ False no caching headers are sent.Currently the middleware does not support non ASCII filenames. If the │ │ │ encoding on the file system happens to be the encoding of the URI it may │ │ │ work but this could also be by accident. We strongly suggest using ASCII │ │ │ only file names for static files.
│ │ │The middleware will guess the mimetype using the Python mimetype │ │ │ @@ -88,15 +88,15 @@ │ │ │ to fallback_mimetype.
│ │ │-
│ │ │
- Parameters │ │ │
-
│ │ │
app – the application to wrap. If you don’t want to wrap an │ │ │ application you can pass it
NotFound
.
│ │ │ exports – a list or dict of exported files and folders.
│ │ │ -disallow – a list of
fnmatch()
rules.
│ │ │ +disallow – a list of
fnmatch()
rules.
│ │ │ cache – enable or disable caching headers.
│ │ │ cache_timeout – the cache timeout in seconds for the headers.
│ │ │ fallback_mimetype – The fallback mimetype for unknown files.
│ │ │
│ │ │
│ │ ├── ./usr/share/doc/python-werkzeug-doc/html/serving.html │ │ │ @@ -130,15 +130,15 @@ │ │ │ exactly like│ │ │SharedDataMiddleware
, it’s actually │ │ │ just wrapping the application in that middleware before │ │ │ serving. │ │ │ │ │ │passthrough_errors – set this to True to disable the error catching. │ │ │ This means that the server will die on errors but │ │ │ it can be useful to hook debuggers in (pdb etc.)
│ │ │ │ │ │ │ │ │ │ │ │ │ │ ├── ./usr/share/doc/python-werkzeug-doc/html/tutorial.html │ │ │ @@ -390,15 +390,15 @@ │ │ │ link_target=link_target, │ │ │ short_id=short_id, │ │ │ click_count=click_count │ │ │ ) │ │ │ssl_context – an SSL context for the connection. Either an │ │ │ -
ssl.SSLContext
, a tuple in the form │ │ │ +ssl.SSLContext
, a tuple in the form │ │ │(cert_file, pkey_file)
, the string'adhoc'
if │ │ │ the server should automatically create one, orNone
│ │ │ to disable SSL (which is the default).Please be aware that redis always works with strings, so you have to convert │ │ │ -the click count to
│ │ │ +the click count toint
by hand.int
by hand. │ │ ││ │ ││ │ │Step 8: Templates¶
│ │ │And here are all the templates. Just drop them into the templates │ │ │ folder. Jinja2 supports template inheritance, so the first thing we will │ │ │ do is create a layout template with blocks that act as placeholders. We │ │ │ also set up Jinja2 so that it automatically escapes strings with HTML │ │ ├── ./usr/share/doc/python-werkzeug-doc/html/unicode.html │ │ │ @@ -103,15 +103,15 @@ │ │ │
There is also the codecs module which provides an open function that decodes │ │ │ automatically from the given encoding.
│ │ ││ │ ││ │ │Error Handling¶
│ │ │Functions that do internal encoding or decoding accept an
│ │ │errors
│ │ │ keyword argument that is passed tostr.decode()
and │ │ │ -str.encode()
. The default is'replace'
so that errors are easy │ │ │ +str.encode()
. The default is'replace'
so that errors are easy │ │ │ to spot. It might be useful to set it to'strict'
in order to catch │ │ │ the error and report the bad data to the client.│ │ ││ │ │Request and Response Objects¶
│ │ │As request and response objects usually are the central entities of Werkzeug │ │ │ powered applications you can change the default encoding Werkzeug operates on │ │ │ @@ -125,34 +125,34 @@ │ │ │ │ │ │ class Response(BaseResponse): │ │ │ charset = 'utf-7' │ │ │
Keep in mind that the error handling is only customizable for all decoding
│ │ │ but not encoding. If Werkzeug encounters an encoding error it will raise a
│ │ │ -UnicodeEncodeError
. It’s your responsibility to not create data that is
│ │ │ +UnicodeEncodeError
. It’s your responsibility to not create data that is
│ │ │ not present in the target charset (a non issue with all unicode encodings
│ │ │ such as utf-8).