There is a server request handler:

import tornado.web from tornado import gen from tornado.ioloop import IOLoop class MainHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.coroutine def post(self): ...do something with request... 

It starts from another file, with the construction:

 import tornado.web from tornado.ioloop import IOLoop from logger import Logger app_settings = dict( debug=True, ) application = tornado.web.Application([ (r"/", MainHandler) ], **app_settings) if __name__ == '__main__': try: object = Logger() application.listen(8888) print('Server Running...') print('Press ctrl + c to close') IOLoop.instance().start() except KeyboardInterrupt: print('Server is out') 

How, using the tornado web framework, to transfer to

 class MainHandler(tornado.web.RequestHandler): 

argument (object), as a parameter? Is it possible to use singleton?

    1 answer 1

    No, singleton is not important. You can transfer this object through Application . You create a new attribute for Application , which assigns your object, and then in RequestHandler get access to your access through the attribute RequestHandler.application.название_вашего_атрибута .

    Here is a simplified example:

     import tornado.ioloop import tornado.web class MyObject(): def __init__(self, value): self.value = value def __str__(self): return self.__class__.__name__ + " " + str(self.value) class MainHandler(tornado.web.RequestHandler): def get(self): obj = self.application.obj self.write(str(obj)) def make_app(): return tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": app = make_app() obj = MyObject(1000) app.obj = obj app.listen(8888) tornado.ioloop.IOLoop.current().start() 

    Naturally MainHandler can be transferred to a separate file.

    PS Do not use the word object for variables, since it is reserved.

    • thank you for your help! - Raikkonen