forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (98 loc) · 3.59 KB
/
main.py
File metadata and controls
123 lines (98 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
# [START functions_concepts_stateless]
# Global variable, modified within the function by using the global keyword.
count = 0
def statelessness(request):
"""
HTTP Cloud Function that counts how many times it is executed
within a specific instance.
Args:
request (flask.Request): The request object.
<http://flask.pocoo.org/docs/1.0/api/#flask.Request>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""
global count
count += 1
# Note: the total function invocation count across
# all instances may not be equal to this value!
return 'Instance execution count: {}'.format(count)
# [END functions_concepts_stateless]
# Placeholder
def heavy_computation():
return time.time()
# Placeholder
def light_computation():
return time.time()
# [START functions_tips_scopes]
# Global (instance-wide) scope
# This computation runs at instance cold-start
instance_var = heavy_computation()
def scope_demo(request):
"""
HTTP Cloud Function that declares a variable.
Args:
request (flask.Request): The request object.
<http://flask.pocoo.org/docs/1.0/api/#flask.Request>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""
# Per-function scope
# This computation runs every time this function is called
function_var = light_computation()
return 'Instance: {}; function: {}'.format(instance_var, function_var)
# [END functions_tips_scopes]
# [START functions_concepts_requests]
def make_request(request):
"""
HTTP Cloud Function that makes another HTTP request.
Args:
request (flask.Request): The request object.
<http://flask.pocoo.org/docs/1.0/api/#flask.Request>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""
import requests
# The URL to send the request to
url = 'http://example.com'
# Process the request
response = requests.get(url)
response.raise_for_status()
return 'Success!'
# [END functions_concepts_requests]
# [START functions_concepts_after_timeout]
def timeout(request):
print('Function running...')
time.sleep(120)
# May not execute if function's timeout is <2 minutes
print('Function completed!')
return 'Function completed!'
# [END functions_concepts_after_timeout]
# [START functions_concepts_filesystem]
def list_files(request):
import os
from os import path
root = path.dirname(path.abspath(__file__))
children = os.listdir(root)
files = [c for c in children if path.isfile(path.join(root, c))]
return 'Files: {}'.format(files)
# [END functions_concepts_filesystem]