7

I'm wondering if it is possible in the Tornado framework to register multiple Application on the same IOLoop ?

Something like

application1 = web.Application([
    (r"/", MainPageHandler),
])
http_server = httpserver.HTTPServer(application1)
http_server.listen(8080)

application2 = web.Application([
    (r"/appli2", MainPageHandler2),
])
http_server2 = httpserver.HTTPServer(application2)
http_server2.listen(8080)

ioloop.IOLoop.instance().start()

Basically I'm trying to structure my webapp so that:

  1. functional applications are separated
  2. multiple handlers with the same purpose (e.g. admin/monitoring/etc) are possible on each webapp
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
oDDsKooL
  • 1,767
  • 20
  • 23
  • Is there any specific reason you need two semantically-separate `Application`s? – Yuval Adam Jun 07 '12 at 15:31
  • well, it's more a functional need than a technical one. basically I wanted to host two applications on the same Tornado container (à la Tomcat); but it seems this isn't the right pattern here. – oDDsKooL Jun 08 '12 at 06:01

1 Answers1

9

The simple thing is if you were to bind your applications to different ports:

...
http_server = httpserver.HTTPServer(application1)
http_server.listen(8080)    # NOTE - port 8080

...
http_server2 = httpserver.HTTPServer(application2)
http_server2.listen(8081)   # NOTE - port 8081

ioloop.IOLoop.instance().start()

This is the base case that Tornado makes easy. The challenge is that by routing to applications at the URI level you're crossing a design boundary which is that each application is responsible for all of the URIs that that are requested by it.

If they all really need to be serviced at the URI level not port, it would probably be best to host different applications on different ports and have Nginx/Apache do the URI routing - anything that involves messing with the Application/Request handling is going to be a world of hurt.

koblas
  • 25,410
  • 6
  • 39
  • 49