Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import json
  2. import tornado.web
  3. import asyncio
  4. import asyncpg
  5. import logging
  6. import tornado.concurrent
  7.  
  8. logger = logging.getLogger('chat.' + __name__)
  9.  
  10.  
  11. def asyncio_coroutine(func):
  12.     func = asyncio.coroutine(func)
  13.     def decorator(*args, **kwargs):
  14.         future = tornado.concurrent.Future()
  15.  
  16.         def future_done(f):
  17.             try:
  18.                 future.set_result(f.result())
  19.             except Exception as e:
  20.                 future.set_exception(e)
  21.  
  22.         asyncio.async(func(*args, **kwargs)).add_done_callback(future_done)
  23.         return future
  24.  
  25.     return decorator
  26.  
  27.  
  28. class BaseHandler(tornado.web.RequestHandler):
  29.     @asyncio.coroutine
  30.     async def _get_engine(self):
  31.         engine = await asyncpg.connect(user='m4bulmagd', database='chatdb', host='localhost', password='123')
  32.         return engine
  33.  
  34.     @asyncio.coroutine
  35.     async def select(self, sqlquery):
  36.         conn = await self._get_engine()
  37.         # with (yield from engine) as conn:
  38.         data = await conn.fetch(sqlquery)
  39.  
  40.         return data
  41.  
  42.     @asyncio.coroutine
  43.     async def insert(self, sqlquery):
  44.         conn = await self._get_engine()
  45.         #with (yield from engine) as conn:
  46.         await conn.execute(sqlquery)
  47.  
  48.  
  49. ############################################################
  50.  
  51. class LoginHandler(BaseHandler):
  52.     @asyncio_coroutine
  53.     def get(self):
  54.         db_messages = yield from self.select('''select * from "user"''')
  55.         self.render("login.html")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement