Skip to content

Commit 7aa1689

Browse files
committed
Merge pull request #259 from GoogleCloudPlatform/background
Add Background Sample
2 parents 573d1e7 + 21a4a56 commit 7aa1689

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

appengine/background/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Using Background Threads from Google App Engine
2+
3+
This example shows how to use manual or basic scaling to start App Engine background threads.
4+
5+
See the [documentation on modules](https://cloud.google.com/appengine/docs/python/modules/) for
6+
more information.
7+
8+
Your app.yaml configuration must specify scaling as either manual or basic. The default
9+
automatic scaling does not allow background threads.

appengine/background/app.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
manual_scaling:
6+
instances: 1
7+
8+
handlers:
9+
- url: .*
10+
script: main.app

appengine/background/main.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
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+
"""
16+
Sample application that demonstrates how to use the App Engine background
17+
threads.
18+
19+
app.yaml scaling must be set to manual or basic.
20+
"""
21+
22+
# [START background-imp]
23+
from google.appengine.api import background_thread
24+
# [END background-imp]
25+
26+
import webapp2
27+
28+
val = 'Dog'
29+
30+
31+
class MainHandler(webapp2.RequestHandler):
32+
def get(self):
33+
self.response.headers['Content-Type'] = 'text/plain'
34+
self.response.write(str(val))
35+
36+
37+
class SetDogHandler(webapp2.RequestHandler):
38+
""" Resets the global val to 'Dog'"""
39+
40+
def get(self):
41+
global val
42+
val = 'Dog'
43+
self.response.headers['Content-Type'] = 'text/plain'
44+
self.response.write('Done')
45+
46+
47+
class SetCatBackgroundHandler(webapp2.RequestHandler):
48+
""" Demonstrates two ways to start new background threads
49+
"""
50+
51+
def get(self):
52+
"""
53+
Demonstrates using a background thread to change the global
54+
val from 'Dog' to 'Cat'
55+
56+
The auto GET parameter determines whether to start the thread
57+
automatically or manually
58+
"""
59+
auto = self.request.get('auto')
60+
61+
# [START background-start]
62+
# sample function to run in a background thread
63+
def change_val(arg):
64+
global val
65+
val = arg
66+
67+
if auto:
68+
# Start the new thread in one command
69+
background_thread.start_new_background_thread(change_val, ['Cat'])
70+
else:
71+
# create a new thread and start it
72+
t = background_thread.BackgroundThread(
73+
target=change_val, args=['Cat'])
74+
t.start()
75+
# [END background-start]
76+
77+
self.response.headers['Content-Type'] = 'text/plain'
78+
self.response.write('Done')
79+
80+
app = webapp2.WSGIApplication([
81+
('/', MainHandler),
82+
('/dog', SetDogHandler),
83+
('/cat', SetCatBackgroundHandler),
84+
], debug=True)
85+
# [END all]

appengine/background/main_test.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
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 main
16+
17+
from mock import patch
18+
import pytest
19+
import webtest
20+
21+
22+
@pytest.fixture
23+
def app(cloud_config, testbed):
24+
main.PROJECTID = cloud_config.project
25+
return webtest.TestApp(main.app)
26+
27+
28+
@patch("main.background_thread")
29+
def test_background(thread, app):
30+
app.get('/dog')
31+
response = app.get('/')
32+
assert response.status_int == 200
33+
assert response.body == 'Dog'
34+
app.get('/cat')
35+
# no stub for system so manually set
36+
main.val = 'Cat'
37+
response = app.get('/')
38+
assert response.status_int == 200
39+
assert response.body == 'Cat'
40+
41+
42+
@patch("main.background_thread")
43+
def test_background_auto_start(thread, app):
44+
app.get('/dog')
45+
response = app.get('/')
46+
assert response.status_int == 200
47+
assert response.body == 'Dog'
48+
app.get('/cat?auto=True')
49+
# no stub for system so manually set
50+
main.val = 'Cat'
51+
response = app.get('/')
52+
assert response.status_int == 200
53+
assert response.body == 'Cat'

0 commit comments

Comments
 (0)