|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""An example of using https://cloud.google.com/profiler on GAE standard.""" |
| 15 | + |
| 16 | +from flask import Flask |
| 17 | +# [START profiler_python_appengine_standard_python37] |
| 18 | +import googlecloudprofiler |
| 19 | + |
| 20 | +# Profiler initialization. It starts a daemon thread which continuously |
| 21 | +# collects and uploads profiles. Best done as early as possible. |
| 22 | +try: |
| 23 | + # service and service_version can be automatically inferred when |
| 24 | + # running on App Engine. project_id must be set if not running |
| 25 | + # on GCP. |
| 26 | + googlecloudprofiler.start(verbose=3) |
| 27 | +except (ValueError, NotImplementedError) as exc: |
| 28 | + print(exc) # Handle errors here |
| 29 | + |
| 30 | +# [END profiler_python_appengine_standard_python37] |
| 31 | + |
| 32 | + |
| 33 | +# If `entrypoint` is not defined in app.yaml, App Engine will look for an app |
| 34 | +# called `app` in `main.py`. |
| 35 | +app = Flask(__name__) |
| 36 | + |
| 37 | + |
| 38 | +@app.route('/') |
| 39 | +def hello(): |
| 40 | + """Return a friendly HTTP greeting.""" |
| 41 | + return 'Hello World!' |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + # This is used when running locally only. When deploying to Google App |
| 46 | + # Engine, a webserver process such as Gunicorn will serve the app. This |
| 47 | + # can be configured by adding an `entrypoint` to app.yaml. |
| 48 | + app.run(host='127.0.0.1', port=8080, debug=True) |
0 commit comments