Search code examples
tornadotornado-motor

How to pass environmental variable (database URL) in Tornado?


I have the elastic URL to be passed as a environmental variable to post method, I have declared the make_session_factory from tornado_sqlalchemy, and passed to requesthandler, how should I receive that in self, below is my code

class MyHandler(RequestHandler):
    _thread_pool = ThreadPoolExecutor(max_workers=10)

    def initialize(self, session_factory): 
      self.session_factory = session_factory

    @gen.coroutine
    def post(self):
        try:
            data = tornado.escape.json_decode(self.request.body)
            yield self.predict('mod1')
        except Exception:
            self.respond('server_error', 500)

    @concurrent.run_on_executor(executor='_thread_pool')
    def _b_run(self, mod, X):
        results = do some calculation
        return results

    @gen.coroutine
    def predict(self, mod):  
        model = mod(load from database)
        values = (load from database)
        results = yield self._b_run(model, values)
        self.respond(results)

    def respond(self, code=200):
        self.set_status(code)
        self.write(json.JSONEncoder().encode({
            "status": code
        }))
        self.finish()


def mod(model):
    #Elastic Client
    es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
    ……...Load model
    return model


factory = make_session_factory(os.environ.get('DATABASE_URL', ''))
define('port', default=8888, help='Tornado port to listen on')

def make_app():  
    url = [(r"/pred", PredictionHandler)]
    return Application(url,session_factory=factory)

if __name__ == "__main__":
    application = make_app()
    http_server = HTTPServer(application)
    http_server.listen(options.port)
    IOLoop.current().start()

For now I have coded the URL in my mod function as localhost, 9200,

To pass that as environment variable I have created the factory and passed the factory as session_factory in my application,

I have to receive that in my requesthandler post method, so that I can pass to my predict method which in tern call my mod function, may I know how to receive that. I tried with initialize but not working properly.

Also found the other option using motor with tornado

import motor.motor_tornado
client = motor.motor_tornado.MotorClient('localhost', 9200)
db = motor.motor_tornado.MotorClient().test_database

application = tornado.web.Application([
    (r'/', MainHandler)
], db=db)

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        db = self.settings['db']

Here also I have to receive in my post method


Solution

  • Any extra arguments you pass to the Application class will be available in self.settings from any of your request handlers.

    So you can modify the initialize method like this:

    def initialize(self): 
        self.session_factory = self.settings['session_factory']
    
    # and just use `self.session_factory` in any other method where you need it