2021-03-09 20:06:23 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from tornado import ioloop, web
|
|
|
|
|
2021-03-24 10:48:25 +00:00
|
|
|
import sprockets_statsd.tornado
|
2021-03-09 20:06:23 +00:00
|
|
|
|
|
|
|
|
2021-03-24 10:48:25 +00:00
|
|
|
class MyHandler(sprockets_statsd.tornado.RequestHandler,
|
2021-03-09 20:06:23 +00:00
|
|
|
web.RequestHandler):
|
|
|
|
async def get(self):
|
|
|
|
with self.execution_timer('some-operation'):
|
|
|
|
await self.do_something()
|
|
|
|
self.set_status(204)
|
|
|
|
|
|
|
|
async def do_something(self):
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
2021-03-24 10:48:25 +00:00
|
|
|
class Application(sprockets_statsd.tornado.Application, web.Application):
|
2021-03-09 20:06:23 +00:00
|
|
|
def __init__(self, **settings):
|
|
|
|
super().__init__([web.url('/', MyHandler)], **settings)
|
|
|
|
|
|
|
|
async def on_start(self):
|
|
|
|
await self.start_statsd()
|
|
|
|
|
|
|
|
async def on_stop(self):
|
|
|
|
await self.stop_statsd()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
app = Application()
|
|
|
|
app.listen(8888)
|
|
|
|
iol = ioloop.IOLoop.current()
|
|
|
|
try:
|
|
|
|
iol.add_callback(app.on_start)
|
|
|
|
iol.start()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
iol.add_future(asyncio.ensure_future(app.on_stop()),
|
|
|
|
lambda f: iol.stop())
|
|
|
|
iol.start()
|