Game snake-use Tornado to build one of the high-performance Web Applications

Source: Internet
Author: User

This new series was designed to record the work in Python. Recently, it was suddenly switched from Microsoft to Linux. net is not very useful, and it was only found in Python for many days. the days under net are really past events ...... Of course, it's just a family statement. It's just a personal feeling. In short, the snake play series is used to specifically record the feeling of working in Python.

Working in. Net advocates integration, integrated IDE, and integrated servers. From the operating system to Webserver, they are all MS inside. It is very suitable for beginners. You don't have to think about anything, and you don't have much to choose. When your capabilities are improved, you will feel that what Microsoft provides is not so perfect. When you are developing, if you do not need the built-in mechanisms of Microsoft, it will make your boss effort to bend around, such as the DataGrid, which is very powerful, but most people will leave their blogs to warn them later, so let's go back and use repeater, what you see is what you get. The editor is also added for compromise.CodeIt became completely unrecognizable and it was barely solved until the emergence of MVC. However, many things and skills accumulated in webform were lost.

I mentioned a lot of dissatisfaction with. net before the official opening.

In other words, python is suitable for developers who are familiar with one language and platform for expansion. For example, they are familiar with the Java platform or. NET developers, of course, are more suitable for C and C ++ developers to occasionally play with web development. Python is fun, but it doesn't mean it is a pretty toy. It is a very productive language on Linux and UNIX platforms. At a very early time, the Linux and BSD systems had built-in python by default. Many integrated tools such as Ubuntu, which are very smart, are compiled in Python. In terms of web development, it is estimated that the circle is small, and many people who are playing python in their spare time are not engaged in web development, so there are not many people who use python to develop Web in China. However, recently, a book has been written on frameworks such as Django. Although it is exposed to Ror, some people are concerned about it.

In fact, the biggest problem in python is not that there are too few choices, but too many choices. Because Python's productivity is too strong, the cost of building a wheel is really low, so there are wheels everywhere, from Quixote, Django, web2py, Web. PY, karrigell ...... There is also uliweb in China ......

You are absolutely dazzled. Maybe the requirements of each application are different, so the starting points of each framework are different, which is a dazzling indicator for beginners, however, the range is wider. Another difference from the Microsoft platform is that the deployment is not integrated, and IIS is mounted to. net, which is actually very popular in the past. Looking at nginx, Lighttpd, the exciting benchmark of the new generation of web servers, and then looking at IIS (those Web servers that advertise their high performance do not compare with IIS during comparative testing, not playing together, not a level ......)

Speaking of performance, I felt like I had a strong performance obsessive-compulsive disorder, and I was seeing a hot eye when I was running fast, so today's pig-Tornado,

This is a framework developed by a company called friend feed acquired by Facebook. In fact, it is also a high-performance WEB server. It is written in pure Python and is open-source, which means that I can improve it myself, another advantage is that the source code can be directly used to solve the problem when the document is abnormal. It turns out that this is very useful. Tornado documentation is very lacking, and there is no API description, the documentation on the official website is basically equivalent to gathering comments in the Code together, but fortunately python is so cute that it is often solved by Dir (object.

Since Facebook was broken down, Tornado's official website was not spared. In fact, the python download page was also broken down. I don't know what's going on. Isn't it because of python.com?

Sacrifice puff, go straight to www.tornado.org, download, decompress, and install:

Python setup. py install

Note: It must be installed in Linux, and there is no epoll in windows, so select is used instead, which is very inefficient. In this case, twisted is also a common scenario.

Tornado itself is a web server, so it can run independently. After using it for two weeks, I felt that it was not actually installed. It was better to directly reference the source code to run it. In many cases, I could easily make some minor changes to the system.

Run python after installation, open the python shell, and enter import tornado

If no error is reported after you press enter, the installation is successful.

Now we can use Tornado to compile the website and create a server. py

The content is as follows:

 
Import tornado. httpserverimport tornado. ioloopimport tornado. webclass mainhandler (tornado. web. requesthandler): def get (Self): Self. write ("Hello, world") Application = tornado. web. application ([(R "/", mainhandler),]) If _ name _ = "_ main _": http_server = tornado. httpserver. httpserver (Application) http_server.listen (8888) Tornado. ioloop. ioloop. instance (). start ()

 

Run Python server. py

At this time, we can see the hello World page in the browser.

Like Django, Tornado uses regular expressions for URL Mapping. However, the difference is that views and models are not forcibly distinguished. Although tornado has a simple database encapsulation, there is no Orm, fortunately, I am not very familiar with Orm, AND THE ORM in python is also very weak, and it is also very easy to write it by myself. The template system is also convenient, but the session is not integrated, but a security cookie is implemented internally. But now many systems do not use the inproc session, so they DIY a session that uses memcached as the backend.

These things will be involved in the next series of post, because there is no intention to write the tutorial, so a lot of details may be reached, for more details, I believe that those who are willing to use this stuff will also have the ability to chew on it.Source codeAnd thousands of rows.

In addition, Tornado is a single process and single-threaded server. Therefore, the code in each requesthandler method is thread-safe.

Many people may wonder if a single process can be faster than a single process? In fact, it is better than words:

However, due to this feature, some delayed operations cannot be written. Currently, Tornado only supports asynchronous curl, and database operations are still blocked, I am studying how to add an asynchronous database operation module to further improve its performance. If there is any result, I will announce it as soon as possible. I also hope that I would like to share with new snake friends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.