When deployed in three lines: recommended for linux or BSD platform to platform reason: because the tornado's high-performance, instead of relying on open multiple processes or threads, stay productive even in a single-process, it is dependent on the epoll linux and BSD Kqueue particularly efficient ps: face questions: the distinction select poll epoll (? ********************************* ) 1 single process open file descriptors (fd file handle). select: 1024 poll: slightly better than a select few, there is no limit epoll: no limit (1 g memory, listening 10W ports) 2 listening socket ways:. select : polling, one by one, checking to see if active, when a linear increase in the socket when the polling rate will be very time consuming poll: to select slightly optimized, just modify the file descriptor, not listening mode change epoll: socket will be connected to the registration epoll, the equivalent roster socket, if there is a socket active, and will be a callback function to notify epoll, hurried over and deal with 3 memory copy mode:. the SELECT: the need for data from copying the user mode kernel mode, this process is time-consuming poll: ditto epoll: shared memory state data and user state data reference blog: HTTPS: //www.cnblogs.com/jeakeven/p/5435916 .html
Start with the first four tornado procedure:. # ## library based tornado Web Import tornado.web # ## tornado efficient the IO core library, which encapsulates the linux epoll module Import tornado.ioloop '' ' the Django in similar view, the CBV mode '' ' class IndexHandler (tornado.web.RequestHandler): # ## get only accept requests DEF get (Self, * args, ** kwargs): Print ( ' get ...... ' ) # ## HttpResponse django in self.write ( ' Hello World! ' ) # ## post request can defPOST (Self, * args, ** kwargs): Pass IF __name__ == ' __main__ ' : # ## of the Application is a web core libraries will instantiate an instance of app # ## app will create a socket, and tied a given port App = tornado.web.Application ([ (R & lt ' / ' , IndexHandler), ]) # ## bound a port, and does not begin listening app.listen (8001 ) '' ' IOLoop.current (): returns a IoLoop instance IOLoop.current () start ():. this begins listening port 8001 '' ' tornado.ioloop.IOLoop.current () Start (). : execution flow 1 bind a port. 2 to start the service and start listening. 3. After the client to a request: 127.0.0.1:8001/ 4 . forwarding the request a route matching 5 after then searches for the corresponding view is processed, processing is complete, return data
V. the HttpServer tornado.httpserver instantiated httperver, and bound port HTTPServer = tornado.httpserver.HTTPServer (App) httpServer.listen ( 8001) # equivalent: app.listen ( 8001 ) from multiple processes: HTTPServer = tornado.httpserver .HTTPServer (App) httpServer.bind ( 8001 ) httpServer.start (num): value: num > 0: num = 5 : 5 open process num< 0: values were based on the value of the number of cpu core computer num: The default is 1 , but we do not recommend using the above method: 1 . If you want to change a process in which the code related, all processes need to be out all the stops 2. monitoring is not convenient (8000--8005 ) based on the above reasons, we recommend using app.listen ( 8000 ) If you want to play multiple ports to monitor practices: the relevant code from the server of multiple clients, then run
Six Options:. Tornado.options: Common methods and properties: Methods: tornado.options.define () Parameters: name: variable name defined default: variable corresponding default value, if too different command line or configuration file passed this corresponding to the value of the variable, then uses the default value type: type variable type mutiple: default is False, showing a plurality of values can be passed to a variable attributes: Options action: in all variables defined define, options can be by get examples: the DEFINE ( " Port" , Default = 8000, int type =, = Help ' port number ' ) DEFINE ( ' names ' , default = [], type = STR, Multiple = True) need to add: tornado.options.parse_command_line () tornado.options .parse_config_file (path): configuration config file will be read into the options the problem: 1 . Python's syntax is not supported by 2 does not support dictionary format. recommended: ordinary config.py: the file as a package using the code: Options ={ " Port " : 8003 , " Host " : ' 127.0.0.1 ' , } when used: Import config Port = config.options.port
Seven routes:. . A acquires a parameter value of the get request: self.get_query_argument () B acquires the request body parameters:. Self.get_body_argument () . In the implementation of C get / prior to post requests, execution will initialize () method DEF the initialize (Self, height): self.height = height Print ( ' the this the initialize IS ' ) (R & lt ' / info / ' , InfoHandler, { " height " : 180 [ }), D analytical reverse URL:. tornado.web.url (R & lt ' / index / ', IndexHandler, name='index'), url = self.reverse_url("index")