/
counter.py
38 lines (30 loc) · 977 Bytes
/
counter.py
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
import logging
import time
import webapp2
from google.appengine.api import runtime
from google.appengine.api import background_thread
GLOBAL_COUNTER = 1
class Shutdown(Exception):
pass
def shutdown():
logging.info("Shutdown hook called")
raise Shutdown
def counter_loop():
global GLOBAL_COUNTER
try:
while GLOBAL_COUNTER < 600:
GLOBAL_COUNTER += 1
time.sleep(1)
logging.info("Shutting down after 600 cycles")
except:
logging.info("Counter loop saw shutdown")
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write('GLOBAL_COUNTER = %d' % GLOBAL_COUNTER)
class StartHandler(webapp2.RequestHandler):
def get(self):
runtime.set_shutdown_hook(shutdown)
thread = background_thread.BackgroundThread(
target=counter_loop)
thread.start()
app = webapp2.WSGIApplication([('/', MainPage), ('/_ah/start/', StartHandler)], debug=True)