|
| 1 | +# Copyright 2018 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 | + |
| 15 | +import time |
| 16 | + |
| 17 | + |
| 18 | +# [START functions_concepts_stateless] |
| 19 | +# Global variable, modified within the function by using the global keyword. |
| 20 | +count = 0 |
| 21 | + |
| 22 | + |
| 23 | +def statelessness(request): |
| 24 | + """ |
| 25 | + HTTP Cloud Function that counts how many times it is executed |
| 26 | + within a specific instance. |
| 27 | + Args: |
| 28 | + request (flask.Request): The request object. |
| 29 | + Returns: |
| 30 | + The response text, or any set of values that can be turned into a |
| 31 | + Response object using `make_response` |
| 32 | + <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>. |
| 33 | + """ |
| 34 | + global count |
| 35 | + count += 1 |
| 36 | + |
| 37 | + # Note: the total function invocation count across |
| 38 | + # all instances may not be equal to this value! |
| 39 | + return 'Instance execution count: {}'.format(count) |
| 40 | +# [END functions_concepts_stateless] |
| 41 | + |
| 42 | + |
| 43 | +def heavy_computation(): |
| 44 | + return time.time() |
| 45 | + |
| 46 | + |
| 47 | +def light_computation(): |
| 48 | + return time.time() |
| 49 | + |
| 50 | + |
| 51 | +# [START functions_tips_scopes] |
| 52 | +# Global (instance-wide) scope |
| 53 | +# This computation runs at instance cold-start |
| 54 | +instance_var = heavy_computation() |
| 55 | + |
| 56 | + |
| 57 | +def scope_demo(request): |
| 58 | + """ |
| 59 | + HTTP Cloud Function that declares a variable. |
| 60 | + Args: |
| 61 | + request (flask.Request): The request object. |
| 62 | + Returns: |
| 63 | + The response text, or any set of values that can be turned into a |
| 64 | + Response object using `make_response` |
| 65 | + <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>. |
| 66 | + """ |
| 67 | + |
| 68 | + # Per-function scope |
| 69 | + # This computation runs every time this function is called |
| 70 | + function_var = light_computation() |
| 71 | + return 'Instance: {}; function: {}'.format(instance_var, function_var) |
| 72 | +# [END functions_tips_scopes] |
| 73 | + |
| 74 | + |
| 75 | +# [START functions_concepts_requests] |
| 76 | +def make_request(request): |
| 77 | + """ |
| 78 | + HTTP Cloud Function that makes another HTTP request. |
| 79 | + Args: |
| 80 | + request (flask.Request): The request object. |
| 81 | + Returns: |
| 82 | + The response text, or any set of values that can be turned into a |
| 83 | + Response object using `make_response` |
| 84 | + <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>. |
| 85 | + """ |
| 86 | + import requests |
| 87 | + |
| 88 | + # The URL to send the request to |
| 89 | + url = 'http://example.com' |
| 90 | + |
| 91 | + # Process the request |
| 92 | + response = requests.get(url) |
| 93 | + response.raise_for_status() |
| 94 | + return 'Success!' |
| 95 | +# [END functions_concepts_requests] |
| 96 | + |
| 97 | + |
| 98 | +# [START functions_concepts_after_timeout] |
| 99 | +def timeout(request): |
| 100 | + print('Function running...') |
| 101 | + time.sleep(120) |
| 102 | + |
| 103 | + # May not execute if function's timeout is <2 minutes |
| 104 | + return 'Done!' |
| 105 | +# [END functions_concepts_after_timeout] |
| 106 | + |
| 107 | + |
| 108 | +# [START functions_concepts_filesystem] |
| 109 | +def list_files(request): |
| 110 | + import os |
| 111 | + from os import path |
| 112 | + |
| 113 | + root = path.dirname(path.abspath(__file__)) |
| 114 | + children = os.listdir(root) |
| 115 | + files = [c for c in children if path.isfile(path.join(root, c))] |
| 116 | + return 'Files: {}'.format(files) |
| 117 | +# [END functions_concepts_filesystem] |
0 commit comments