58

I have several questions about Tornado and other web frameworks.

1) Tornado claims to be a webserver (a non-blocking one, therefore much performant), so some people said it does not play the role of django --i.e., they say tornado is not a web framework.

However, it does provide a web framework I think (http://www.tornadoweb.org/documentation#main-modules) -- in this way, it seems to replace django as the web development framework.

Is my above understanding correct?

2) Normally, several Tornados are set up behind Nginx. Tomcat is also normally set up behind Apache web server. Can I say Tornado plays exactly same role of Tomcat does for Java web server? If the answer is yes, then Tornado IS a web framework.

3) I read some article saying using Tornado and Django together, such as http://www.jeremybowers.com/blog/post/3/on-deploying-tornado-web-server-framework/, but I read some article online claiming that "if you use Django, then you lose the asynchronous from Tornado", is this true or false? A related question though, if Tornado is itself a web framework as I said in 1), why people bother using Django at all? (to result the plugin?)

Can someone give me a 101 introduction?

mdml
  • 22,442
  • 8
  • 58
  • 66
chen
  • 4,302
  • 6
  • 41
  • 70

2 Answers2

46

To answer the question,

  • NO, Tornado is not a replacement to Django. It's an alternative.

  • YES, they are complementary to each other but not in the same process (*)

I would use Django when it's a big team effort and/or needs to run on a SQL database.

I would use Tornado for everything else. Ie. personal projects, WebSocket-related apps, or apps that use a NoSQL backend.

(*) Don't bother running Django inside Tornado or the other way around unless you really have a strong requirement for that.

Rafid Aslam
  • 305
  • 3
  • 10
Peter Bengtsson
  • 7,235
  • 8
  • 44
  • 53
  • 9
    Hi! I'm a bit late, but I was wondering why not bothering running django inside tornado? What if I wanted to handle websocket requests using tornado and accessing mysql database through django's api? Would that be part of "running django inside tornado"? Thanks – Sam Mar 13 '13 at 00:42
  • Why personal projects? Cos because you want to learn it, it is not a valid reason tornado over django – Tolo Palmer Aug 21 '17 at 09:40
33
  1. Tornado is a web server and a web framework by most definitions, but it's quite a minimal framework (compared to Rails or Django). Tornado modules are loosely coupled, so it's possible to use just the web server component (or even just the lower level IO loop). Compared to Django, there are a lot of areas where Tornado doesn't have as much functionality. For example, there isn't really a model abstraction in Tornado, you have to roll your own.

  2. When you run a Tornado app behind Nginx, that's your app running – Nginx just proxies to it. I believe Tomcat is an application server, distinct from your application. I wouldn't call Tomcat a web framework.

  3. [Update: Django added support for async views in 3.0 and some ORM queries in 4.0. This point is no longer accurate] Django is not asynchronous, so generally your app will block while using the Django components. This may not be a big deal, it depends what you're doing. The Tornado devs have stated (paraphrasing heavily) that for most applications, the biggest win comes from not blocking while waiting for the client, i.e. web browser. Blocking on the database, for example, is not a big deal if you keep your queries fast.

There are a lot of pros and cons for both Django and Tornado, and there are many alternatives to both - it's definitely not just a choice between the two. Here's a very quick list of why you might want to use Django though:

Pros for Django:

  • it's a fuller stack (admin pages for example are very easy to implement)
  • it's much more established (plugins, tutorials, etc.)
  • it's better documented
  • its ORM is very nice
Cole Maclean
  • 5,627
  • 25
  • 37
  • 13
    To me, a big Tornado fan, a MAJOR plus for Django is it's ORM. If you have a SQL database, use Django. – Peter Bengtsson Jun 08 '11 at 15:05
  • Agreed, I added a bullet for that. – Cole Maclean Jun 08 '11 at 20:54
  • 3
    You have a great ORM you can use very in any python project: SQLAlchemy. So you won't really miss that. The biggest drawback of tornado is for me the lack of session, you have to implement them yourself (not a big task but it would be better to have a standard library). – Florent Jan 20 '14 at 01:43
  • @colemaclean, Can not agree on your comment "Blocking on the database, for example, is not a big deal if you keep your queries fast.". How do you decide a call is fast enough? How do you get sure a query will keep the same response time on every call? When you develop a system, how do you get sure it will give the same performance under preasure? – cagatay Nov 08 '14 at 08:25
  • @cagatay Well, those are all judgement calls. You can't be 100% sure of any of that. But, making the db queries async doesn't really change that. I'll add a link to the dev comments. – Cole Maclean Nov 13 '14 at 10:47
  • 1
    update: I believe django >=3.0 allows asynchronous functions by now: https://docs.djangoproject.com/en/3.1/topics/async/ – riggedCoinflip Jan 13 '21 at 16:31