39
39
40
40
# [END functions_tips_connection_pooling]
41
41
42
+ from functools import reduce
43
+
42
44
43
45
# Placeholder
44
46
def file_wide_computation ():
@@ -50,6 +52,42 @@ def function_specific_computation():
50
52
return 1
51
53
52
54
55
+ def light_computation ():
56
+ numbers = list (range (1 , 10 ))
57
+ return reduce (lambda x , t : t + x , numbers )
58
+
59
+
60
+ def heavy_computation ():
61
+ # Multiplication is more computationally expensive than addition
62
+ numbers = list (range (1 , 10 ))
63
+ return reduce (lambda x , t : t * x , numbers )
64
+
65
+
66
+ # [START functions_tips_global_scope]
67
+ # [START run_tips_global_scope]
68
+ # Global (instance-wide) scope
69
+ # This computation runs at instance cold-start
70
+ instance_var = heavy_computation ()
71
+
72
+
73
+ def scope_demo (request ):
74
+ """
75
+ HTTP Cloud Function that declares a variable.
76
+ Args:
77
+ request (flask.Request): The request object.
78
+ <http://flask.pocoo.org/docs/1.0/api/#flask.Request>
79
+ Returns:
80
+ The response text, or any set of values that can be turned into a
81
+ Response object using `make_response`
82
+ <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
83
+ """
84
+ function_var = light_computation ()
85
+ return 'Per instance: {}, per function: {}' .format (
86
+ instance_var , function_var )
87
+ # [END run_tips_global_scope]
88
+ # [END functions_tips_global_scope]
89
+
90
+
53
91
# [START functions_tips_lazy_globals]
54
92
# Always initialized (at cold-start)
55
93
non_lazy_global = file_wide_computation ()
0 commit comments