File: tornado_example.py

package info (click to toggle)
aiocache 0.12.3-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 692 kB
  • sloc: python: 5,044; makefile: 221; sh: 7
file content (24 lines) | stat: -rw-r--r-- 812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tornado.web
import tornado.ioloop
from datetime import datetime
from aiocache import cached
from aiocache.serializers import JsonSerializer


class MainHandler(tornado.web.RequestHandler):

    # Due some incompatibilities between tornado and asyncio, caches can't use the "timeout" feature
    # in order to make it work, you will have to specify it always to 0
    @cached(key="my_custom_key", serializer=JsonSerializer(), timeout=0)
    async def time(self):
        return {"time": datetime.now().isoformat()}

    async def get(self):
        self.write(await self.time())


if __name__ == "__main__":
    tornado.ioloop.IOLoop.configure('tornado.platform.asyncio.AsyncIOLoop')
    app = tornado.web.Application([(r"/", MainHandler)])
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()