From f68f856e98548409ddf9f3ac1325d08a214efdea Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 9 Oct 2019 15:03:22 -0500 Subject: [PATCH 1/7] feat: Add serverless scope sample, 142060621 --- functions/tips/main.py | 38 +++++++++++++++++++++++++++++++++++++ functions/tips/main_test.py | 5 +++++ 2 files changed, 43 insertions(+) diff --git a/functions/tips/main.py b/functions/tips/main.py index 3e61cb1c26c..2bfecc03b49 100644 --- a/functions/tips/main.py +++ b/functions/tips/main.py @@ -39,6 +39,7 @@ # [END functions_tips_connection_pooling] +from functools import reduce # Placeholder def file_wide_computation(): @@ -50,6 +51,16 @@ def function_specific_computation(): return 1 +def light_computation(): + numbers = list(range(1, 9)) + return reduce(lambda x, t: t + x, numbers) + +def heavy_computation(): + # Multiplication is more computationally expensive than addition + numbers = list(range(1, 9)) + return reduce(lambda x, t: t * x, numbers) + + # [START functions_tips_lazy_globals] # Always initialized (at cold-start) non_lazy_global = file_wide_computation() @@ -57,6 +68,33 @@ def function_specific_computation(): # Declared at cold-start, but only initialized if/when the function executes lazy_global = None +# [START functions_tips_scopes] +# [START run_tips_global_scope] +# Global (instance-wide) scope +# This computation runs at instance cold-start +instance_var = heavy_computation() +# [END functions_tips_global_scope] +# [END run_tips_global_scope] + +# [START functions_tips_global_scope] +# [START run_tips_global_scope] +def scope_demo(request): + """ + HTTP Cloud Function that declares a variable. + Args: + request (flask.Request): The request object. + + Returns: + The response text, or any set of values that can be turned into a + Response object using `make_response` + . + """ + function_var = light_computation() + print('Per instance: {}, per function: {}'.format(instance_var, function_var)) + return +# [END functions_tips_global_scope] +# [END run_tips_global_scope] + def lazy_globals(request): """ diff --git a/functions/tips/main_test.py b/functions/tips/main_test.py index aacfea6a1ea..af800bec5d6 100644 --- a/functions/tips/main_test.py +++ b/functions/tips/main_test.py @@ -35,6 +35,11 @@ def test_lazy_globals(app): main.lazy_globals(flask.request) +def test_scope_demo(app): + main.scope_demo(flask.request) + assert f"Per instance: 362880, per function: 45" in out + + @responses.activate def test_connection_pooling_200(app): responses.add(responses.GET, 'http://example.com', From ec83131cb63c58be152ad68e6ff143426675105d Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 9 Oct 2019 15:11:15 -0500 Subject: [PATCH 2/7] style: linter issues --- functions/tips/main.py | 6 +++--- functions/tips/main_test.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/functions/tips/main.py b/functions/tips/main.py index 2bfecc03b49..b67604340f4 100644 --- a/functions/tips/main.py +++ b/functions/tips/main.py @@ -45,12 +45,10 @@ def file_wide_computation(): return 1 - # Placeholder def function_specific_computation(): return 1 - def light_computation(): numbers = list(range(1, 9)) return reduce(lambda x, t: t + x, numbers) @@ -76,6 +74,7 @@ def heavy_computation(): # [END functions_tips_global_scope] # [END run_tips_global_scope] + # [START functions_tips_global_scope] # [START run_tips_global_scope] def scope_demo(request): @@ -90,7 +89,8 @@ def scope_demo(request): . """ function_var = light_computation() - print('Per instance: {}, per function: {}'.format(instance_var, function_var)) + print('Per instance: {}, per function: {}'.format( + instance_var, function_var)) return # [END functions_tips_global_scope] # [END run_tips_global_scope] diff --git a/functions/tips/main_test.py b/functions/tips/main_test.py index af800bec5d6..47bb6264bc3 100644 --- a/functions/tips/main_test.py +++ b/functions/tips/main_test.py @@ -35,7 +35,8 @@ def test_lazy_globals(app): main.lazy_globals(flask.request) -def test_scope_demo(app): +def test_scope_demo(capsys): + out, _ = capsys.readouterr() main.scope_demo(flask.request) assert f"Per instance: 362880, per function: 45" in out From a43a3ca9a921bd0df2e63c77a196be8ad359ba77 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 9 Oct 2019 15:51:02 -0500 Subject: [PATCH 3/7] fix: lint nits --- functions/tips/main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/functions/tips/main.py b/functions/tips/main.py index b67604340f4..1e0f8785a36 100644 --- a/functions/tips/main.py +++ b/functions/tips/main.py @@ -41,18 +41,22 @@ from functools import reduce + # Placeholder def file_wide_computation(): return 1 + # Placeholder def function_specific_computation(): return 1 + def light_computation(): numbers = list(range(1, 9)) return reduce(lambda x, t: t + x, numbers) + def heavy_computation(): # Multiplication is more computationally expensive than addition numbers = list(range(1, 9)) From 1375358489eb1aeb48915c35e0e07aa3ed7d133f Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 9 Oct 2019 16:00:55 -0500 Subject: [PATCH 4/7] fix: correct test values for the functions scope demo --- functions/tips/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/tips/main.py b/functions/tips/main.py index 1e0f8785a36..0c36769a6e3 100644 --- a/functions/tips/main.py +++ b/functions/tips/main.py @@ -53,13 +53,13 @@ def function_specific_computation(): def light_computation(): - numbers = list(range(1, 9)) + numbers = list(range(1, 10)) return reduce(lambda x, t: t + x, numbers) def heavy_computation(): # Multiplication is more computationally expensive than addition - numbers = list(range(1, 9)) + numbers = list(range(1, 10)) return reduce(lambda x, t: t * x, numbers) From 1cbb628ef4eac0fb7e43dc5485934b8920cf6bd8 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 9 Oct 2019 16:10:38 -0500 Subject: [PATCH 5/7] fix type of test --- functions/tips/main_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/functions/tips/main_test.py b/functions/tips/main_test.py index 47bb6264bc3..e4fb31ac753 100644 --- a/functions/tips/main_test.py +++ b/functions/tips/main_test.py @@ -35,10 +35,10 @@ def test_lazy_globals(app): main.lazy_globals(flask.request) -def test_scope_demo(capsys): - out, _ = capsys.readouterr() - main.scope_demo(flask.request) - assert f"Per instance: 362880, per function: 45" in out +def test_scope_demo(app): + with app.test_request_context(): + res = main.scope_demo(flask.request) + assert res == 'Per instance: 362880, per function: 45' @responses.activate From c9d2ab893f6b566f06d1a776070879fd32f054c7 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 9 Oct 2019 16:21:23 -0500 Subject: [PATCH 6/7] fix return --- functions/tips/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/functions/tips/main.py b/functions/tips/main.py index 0c36769a6e3..df0f8802f41 100644 --- a/functions/tips/main.py +++ b/functions/tips/main.py @@ -93,9 +93,8 @@ def scope_demo(request): . """ function_var = light_computation() - print('Per instance: {}, per function: {}'.format( - instance_var, function_var)) - return + return 'Per instance: {}, per function: {}'.format( + instance_var, function_var) # [END functions_tips_global_scope] # [END run_tips_global_scope] From af90b8a56fe20e32c05f4394d8bf9a522ddfd7f9 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Thu, 10 Oct 2019 12:41:58 -0300 Subject: [PATCH 7/7] Update main.py --- functions/tips/main.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/functions/tips/main.py b/functions/tips/main.py index df0f8802f41..8196e0db371 100644 --- a/functions/tips/main.py +++ b/functions/tips/main.py @@ -63,24 +63,13 @@ def heavy_computation(): return reduce(lambda x, t: t * x, numbers) -# [START functions_tips_lazy_globals] -# Always initialized (at cold-start) -non_lazy_global = file_wide_computation() - -# Declared at cold-start, but only initialized if/when the function executes -lazy_global = None - -# [START functions_tips_scopes] +# [START functions_tips_global_scope] # [START run_tips_global_scope] # Global (instance-wide) scope # This computation runs at instance cold-start instance_var = heavy_computation() -# [END functions_tips_global_scope] -# [END run_tips_global_scope] -# [START functions_tips_global_scope] -# [START run_tips_global_scope] def scope_demo(request): """ HTTP Cloud Function that declares a variable. @@ -95,8 +84,16 @@ def scope_demo(request): function_var = light_computation() return 'Per instance: {}, per function: {}'.format( instance_var, function_var) -# [END functions_tips_global_scope] # [END run_tips_global_scope] +# [END functions_tips_global_scope] + + +# [START functions_tips_lazy_globals] +# Always initialized (at cold-start) +non_lazy_global = file_wide_computation() + +# Declared at cold-start, but only initialized if/when the function executes +lazy_global = None def lazy_globals(request):